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.
InputThe 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.

OutputThere 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 这道题和之前一道题类似,都是多个字符串匹配问题,只是这道题多了倒序的匹配。直接reverse就可以了
遍历第一个串的所有后缀,然后匹配每个字符串的正反序。得到公共匹配的最小。然后枚举所有后缀取最大
 #include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
int _,n,Next[];
vector<string> t; void prekmp(string s) {
int len=s.size();
int i,j;
j=Next[]=-;
i=;
while(i<len) {
while(j!=-&&s[i]!=s[j]) j=Next[j];
Next[++i]=++j;
}
} int kmp(string t,string p) {
int lent=t.size(),lenp=p.size();
int i=,j=,ans=-,res;
res=-;
while(i<lent) {
while(j!=-&&t[i]!=p[j]) j=Next[j];
++i;++j;
res=max(res,j);
}
ans=max(ans,res);
reverse(t.begin(),t.end());
res=-;
i=,j=;
while(i<lent) {
while(j!=-&&t[i]!=p[j]) j=Next[j];
++i;++j;
res=max(res,j);
}
ans=max(ans,res);
return ans;
} int main() {
// freopen("in","r",stdin);
for(scanf("%d",&_);_;_--) {
scanf("%d",&n);
string s;
for(int i=;i<n;i++) {
cin>>s;
t.push_back(s);
}
string str=t[],tempstr;
int len=str.size(),maxx=-;
for(int i=;i<len;i++) {
tempstr=str.substr(i,len-i);
int ans=0x3f3f3f;
prekmp(tempstr);
for(int j=;j<t.size();j++) {
ans=min(kmp(t[j],tempstr),ans);
}
maxx=max(maxx,ans);
}
printf("%d\n",maxx);
t.clear();
}
}

最新文章

  1. 下订单存储过程 - MYSQL
  2. 关于java连接mysql数据库的几个问题的解决方法。
  3. 无法启动此程序,因为计算机中丢失MSVCP110.dll
  4. HDU 2136 Largest prime factor 參考代码
  5. jquery.prompt.js 弹窗的使用
  6. win32 api 文件操作!
  7. UVA - 10057 A mid-summer night&amp;#39;s dream.
  8. C语言学生管理系统(增进版)
  9. Unity3D input.GetAxis
  10. geotrellis使用(四十二)将 Shp 文件转为 GeoJson
  11. 在 django模型中封装元组和字典, 字段中使用chioce参数实现数据的一一对应
  12. 数据结构与算法-图的最短路径Dijkstra
  13. 2nd 历年学生作品评论(3部)
  14. [java]Stream API——collect、reduce、orElse(x)
  15. 带领技术小白入门——基于java的微信公众号开发(包括服务器配置、java web项目搭建、tomcat手动发布web项目、微信开发所需的url和token验证)
  16. 九度OJ 1262:Sequence Construction puzzles(I)_构造全递增序列 (DP)
  17. 【bzoj2819】Nim DFS序+树状数组+倍增LCA
  18. PAT (Basic Level) Practise (中文)-1033. 旧键盘打字(20)
  19. sql server 中引號嵌套
  20. BAT经典面试题,深入理解Java内存模型JMM

热门文章

  1. libstdc++.so.6
  2. Python多进程-进程间数据的传递
  3. react过渡动画效果的实现,react-transition-group
  4. SqlServer——判断对象是否存在
  5. leetcode241
  6. 7-EasyNetQ之Request &amp; Response
  7. android-auto-scroll-view-pager (无限广告轮播图)
  8. NLP整体流程的代码
  9. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)
  10. GNU Gettext