Description

You are given a number of case-sensitive strings of alphabetic characters, find the largest string X, such that either X, or its inverse can be found as a substring of any of the given strings.

Input

The first line of the input contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains a single integer n (1 <= n <= 100), the number of given strings, followed by n lines, each representing one string of minimum length 1 and maximum length 100. There is no extra white space before and after a string.

Output

There should be one line per test case containing the length of the largest string found.

Sample Input

  1. 2
  2. 3
  3. ABCD
  4. BCDFF
  5. BRCD
  6. 2
  7. rose
  8. orchid

Sample Output

  1. 2
  2. 2
  3.  
  4. 题目大意:
    给你n个字符串 要你找这n个字符串中最大的相同字串(字符串可以逆置)
  5.  
  6. 分析:
    先找到最小的母串 然后这个母串的所有字串从大到小 都和n个串循环一遍 只要找到符合的答案就跳出
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include<iostream>
  5. #include<algorithm>
  6. #include<math.h>
  7. #include<queue>
  8. #define N 220
  9. #define INF 0xffffffff
  10. using namespace std;
  11.  
  12. int main()
  13. {
  14. int T,n;
  15. char str[N][N],s1[N],s2[N];
  16. scanf("%d",&T);
  17. while(T--)
  18. {
  19. scanf("%d",&n);
  20. int Min=INF,b;
  21. for(int i=; i<n; i++)
  22. {
  23. scanf("%s",str[i]);
  24. if(strlen(str[i])<Min)
  25. {
  26. Min=strlen(str[i]);
  27. b=i;
  28. }
  29. }
  30. int len=Min,flag;
  31. while(len)
  32. {
  33. for(int i=; i<=Min-len; i++)
  34. {
  35. flag=;
  36. strncpy(s1,str[b]+i,len);
  37. int k=;
  38. for(int i=len-; i>=; i--)
  39. {
  40. s2[k++]=s1[i];
  41. }
  42. s1[len]=s2[len]='\0';
  43. for(int i=; i<n; i++)
  44. {
  45. if(strstr(str[i],s1)==NULL && strstr(str[i],s2)==NULL)
  46. {
  47. flag=;
  48. break;
  49. }
  50. }
  51. if(flag==)
  52. {
  53. break;
  54. }
  55. }
  56. if(flag==)
  57. break;
  58. len--;
  59. }
  60. printf("%d\n",len);
  61. }
  62. return ;
  63. }

Substrings--poj1226(字符串)的更多相关文章

  1. Codeforces Round #306 (Div. 2) A. Two Substrings【字符串/判断所给的字符串中是否包含不重叠的“BA” “AB”两个字符串】

    A. Two Substrings time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  2. hihocoder #1152 Lucky Substrings 【字符串处理问题】strsub()函数+set集合去重

    #1152 : Lucky Substrings时间限制:10000ms单点时限:1000ms内存限制:256MB描述A string s is LUCKY if and only if the nu ...

  3. Substrings 子字符串-----搜索

    Description You are given a number of case-sensitive strings of alphabetic characters, find the larg ...

  4. Ruby:字符串处理函数

    字符串处理函数1.返回字符串的长度 str.length => integer 2.判断字符串中是否包含另一个串 str.include? other_str => true or fal ...

  5. Codeforces Round #258 (Div. 2)-(A,B,C,D,E)

    http://blog.csdn.net/rowanhaoa/article/details/38116713 A:Game With Sticks 水题.. . 每次操作,都会拿走一个横行,一个竖行 ...

  6. 2019NC#1

    LINK B Integration 题意: 给定$a_1,a_2,...,a_n$, 计算 $$\frac{1}{π}\int_{0}^{\infty}\frac{1}{\prod\limits_{ ...

  7. POJ1226 Substrings ——后缀数组 or 暴力+strstr()函数 最长公共子串

    题目链接:https://vjudge.net/problem/POJ-1226 Substrings Time Limit: 1000MS   Memory Limit: 10000K Total ...

  8. [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  9. LA4671 K-neighbor substrings(FFT + 字符串Hash)

    题目 Source http://acm.hust.edu.cn/vjudge/problem/19225 Description The Hamming distance between two s ...

  10. 字符串(后缀数组):POJ 3415 Common Substrings

    Common Substrings   Description A substring of a string T is defined as: T(i, k)=TiTi+1...Ti+k-1, 1≤ ...

随机推荐

  1. Clean Code 第十章 : 类

    最近的CleanCode读到了第十章.这一张主要讲了如何去构造一个类,感觉的CleanCode至此已经不仅仅是单纯的讲如何'写'出漂亮的代码,而是从设计方向上去构造出好的代码了. 本章节主要讲了: * ...

  2. casting in C++

    这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=39 February 20, 2013 casting in C++ Fil ...

  3. 初探ABP--记一些常见的开发问题

    1.Update-Database : 无法将“Update-Database”项识别为 cmdlet.函数.脚本文件或可运行程序的名称.请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次. ...

  4. sublime text3前端开发插件配置以及使用(个人喜爱)

    第一步下载软件接着Ctrl +~ (回车)把下面安装包管理添加到sublimeimport urllib.request,os; pf = 'Package Control.sublime-packa ...

  5. codevs 2600 13号星期几?

    时间限制: 1 s  空间限制: 8000 KB  题目等级 : 黄金 Gold 题目描述 Description 从1900年1月1日(星期一) 开始经过的n年当中,每个月的13号这一天是星期一.星 ...

  6. DAX:New and returning customers

    The New and Returning Customers pattern dynamically calculates the number of customers with certain ...

  7. Android(java)学习笔记164:开发一个多界面的应用程序之不同界面间互相传递数据(短信助手案例)

    1.首先我们看看下面这个需求: 这里我们在A界面上,点击这个按钮"选择要发送的短信",开启B界面上获取网络上各种短信祝福语,然后B界面会把这些网络祝福语短信发送给A界面到" ...

  8. Encryption requires the OpenSSL PHP extension 报错

    报错截图: 解决办法: 修改php.ini配置文件,打开该拓展 open php.ini search “opensll” remove the semicolon from: extension=p ...

  9. Visual Studio 安装VS10x CodeMAP

    最近出差,用的是公司电脑,电脑安装的是Visual Studio 2017 VS10x CodeMap 支持Visual Studio 2010, 2012, 2013, 2015,不支持Visual ...

  10. 获得Dictionary所有key和value值

    Dictionary<string, string> dc = new Dictionary<string, string>(); dc.Add("code" ...