http://acm.hdu.edu.cn/showproblem.php?pid=1238

Substrings

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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 file 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

2
3
ABCD
BCDFF
BRCD
2
rose
orchid
 

Sample Output

2 2

之前写个是完全的暴力写的,因为数据很小,暴力也没关系,这次用了KMP,有点小题大做的感觉,不过刚刚学,就当练习了。

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std; #define MOD 10000
#define N 210 char s[N][N];
int Next[N]; void FindNext(char b[])
{
int i=, j=-, blen=strlen(b);
Next[] = -; while(i<blen)
{
if(j==- || b[i]==b[j])
Next[++i] = ++j;
else
j = Next[j];
}
} int KMP(char a[], char b[])
{
int i=, j=;
int alen=strlen(a), blen=strlen(b); FindNext(b); while(i<alen)
{
while(j==- || (a[i]==b[j] && i<alen && j<blen))
i++, j++;
if(j==blen)
return ;
j = Next[j];
}
return ;
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int i, j, k, n, MinLen=, len;
char ss[N]; memset(s, , sizeof(s)); scanf("%d", &n); for(i=; i<n; i++)
{
scanf("%s", s[i]);
len = strlen(s[i]); if(len<MinLen)
{
MinLen = len;
memset(ss, , sizeof(ss));
strcpy(ss, s[i]);
}
} char a[N], b[N];
int index=; for(i=MinLen; i>; i--)
for(j=; j<=MinLen-i; j++)
{
memset(a, , sizeof(a));
memset(b, , sizeof(b));
strncpy(a, ss+j, i);
strcpy(b, a);
strrev(b); for(k=; k<n; k++)
{
if(KMP(s[k], a)== && KMP(s[k], b)==)
break;
} if(k==n)
{
index = i;
i=-, j=;
}
} if(index)
printf("%d\n", index);
else
printf("0\n"); }
return ;
}

最新文章

  1. MVC中RenderBody的工作原理
  2. 如何成为python高手
  3. 16s及宏基因组测序公司资源--20161104
  4. 编译安装GCC 4.7.2
  5. DNS服务器配置
  6. PySe-002-Py-简单示例及编码设定
  7. SPOJ #692. Fruit Farm
  8. 《第一行代码--Android》阅读笔记之界面设计
  9. Redis 服务器
  10. 在Jersey中如何处理泛型集合
  11. time_wait和clost_wait说明
  12. 从数据库提取数据通过jstl显示在jsp页面上
  13. C++学习7-面向对象编程基础(多态性与虚函数、 IO文件流操作)
  14. es7 class装饰器
  15. [development][vim] vim显示空白字符
  16. 廖雪峰Java4反射与泛型-3范型-5extends通配符
  17. SpringMVC源码阅读:定位Controller
  18. 元素设置disabled属性后便无法向后台传值
  19. 反射:获取Class对象的三种方式
  20. 关于浏览器localhost点击wamp项目跳转出错

热门文章

  1. mysqldump之不老将
  2. JAVA中request.getParameterMap()用法笔记
  3. 查询中mybatis的if判断里传入0
  4. JDA 8.0.0.0小版本升级
  5. 149. Max Points on a Line (Array; Greedy)
  6. 123. Best Time to Buy and Sell Stock III (Array; DP)
  7. Too Rich(贪心+DFS)
  8. JS 图片切换
  9. git pull和git fetch命令
  10. vue2.0学习小列子