HDU1238 Substrings(kmp,最长公共子串)
来源:互联网 发布:手机淘宝秒杀需要刷新 编辑:程序博客网 时间:2024/06/05 22:41
Problem 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
23ABCDBCDFFBRCD2roseorchid
Sample Output
22
思路
求很多串的最长公共子串,串可以翻转,和POJ3080差不多
代码
#include<cstdio>#include<cstring>#include<string>#include<set>#include<iostream>#include<stack>#include<queue>#include<vector>#include<algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define mod 10000007#define debug() puts("what the fuck!!!")#define N 100+20#define ll longlongusing namespace std;int t,n;int nxt[N];string str[150];void get_next(string s){ int len=s.length(); int j=0,k=-1; nxt[0]=-1; while(j<len) if(k==-1||s[j]==s[k]) nxt[++j]=++k; else k=nxt[k];}bool kmp(string p,string s)//判断s是不是p的子串{ int plen=p.length(); int slen=s.length(); int i=0,j=0; while(i<plen&&j<slen) { if(j==-1||p[i]==s[j]) { i++; j++; } else j=nxt[j]; } if(j==slen) return true; return false;}int pd(string s){ int flag=0; get_next(s); for(int k=1; k<n; k++) if(!kmp(str[k],s)) { flag=1; break; } if(!flag) return 1; else return 0;}int main(){ ios::sync_with_stdio(false); cin.tie(0); cin>>t; while(t--) { cin>>n; for(int i=0; i<n; i++) cin>>str[i]; string ans=""; for(int i=1; i<=str[0].size(); i++) { for(int j=0; j<=str[0].size()-i; j++) { string op=str[0].substr(j,i); if(pd(op)) { if(ans.size()<op.size()) ans=op; } reverse(op.begin(),op.end()); if(pd(op)) { if(ans.size()<op.size()) ans=op; } } } cout<<ans.size()<<endl; } return 0;}
- HDU1238 Substrings(kmp,最长公共子串)
- HDU1238 POJ1226 Substrings 最长公共子串(子反串也算)
- HDU1238 最长公共子序列
- hdu1238——Substrings——————【求多个字串的最长公共子串】
- HDU1238 Substrings(KMP+穷举)
- 最长公共子串(1)--hdu1238(多个字符串得最长公共子串--暴力)
- poj1226 Substrings (多串最长公共子串+正反)
- HDU 1238 Substrings (最长公共子串+DFS)
- 最长公共子串(Longest Common SubStrings)
- poj1226 kmp 最长公共子串
- Codevs_P1425 最长公共子串(KMP)
- POJ 1226 Substrings(最长公共连续串的变形,可以倒转+KMP)
- HDU1238 Substrings
- hdu1238 Substrings
- HDU1238:Substrings
- HDU1238 Substrings
- hdu1238---Substrings
- hdu1238 Substrings
- 正则表达式之道
- jmeter请求数据加密在发送方法
- c++ 常见问题
- Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程(附源码)
- 文章中插入Visio及Matlab矢量图
- HDU1238 Substrings(kmp,最长公共子串)
- C++ operator关键字(重载操作符)
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 Trig Function(数据水,暴力+逆元)
- 本地访问云主机jupyter_notebook
- EventBus使用详解
- 摆摊(stall)
- 求一个n阶方阵对角线元素之和。
- 一个web项目web.xml的配置中<context-param>配置作用
- FZU1686 神龙的难题 —— Dancing Links 可重复覆盖