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

题意:给你n个串,求一个子串,这个子串在所有串中都出现,或者在逆串中出现。求最大的这个子串长度。

题解:分析一下,这个子串肯定会在最短的串中出现,所以,枚举最小串的所有子串,并且从最大的子串开始,看看这个子串是否出现在剩余的所有串中。查询剩余串的话,可以用KMP搞一下。

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#define N 205
using namespace std;
int next[N];
int n;
char s1[N];//模板串
char s2[N];//主串
int len1,len2,ans;
char str[][N];
void getnext(int plen){
int i = ,j = -;
next[]=-;
while( i<plen ){
if(j==-||s1[i]==s1[j]){
++i;++j;
if(s1[i]!=s1[j] )
next[i] = j;
else
next[i] = next[j];
}
else
j = next[j];
}
}
bool KMP(){
int i = ,j = ;
while (i<len2&&j<len1){
if( j == - || s2[i] == s1[j] ){
++i;
++j;
}
else {
j = next[j];
}
}
if(j>=len1)return true;
else
return false;
}
int main(){
int cas;
scanf("%d",&cas);
while(cas--){
scanf("%d",&n);
int pos=-,minn=,ans=;
bool flag=false;
for(int i=;i<=n;i++){
scanf("%s",&str[i]);
int len=strlen(str[i]);
if(len<minn){
minn=len;pos=i;
}
}
for(int i=minn;i>=;i--){
for(int j=;j+i<=minn;j++){
memset(s1,,sizeof(s1));
for(int k=j;k<j+i;k++){
s1[k-j]=str[pos][k];
}
// printf("**%s\n",s1);
len1=i;
getnext(i);
int g;
for(g=;g<=n;g++){
strcpy(s2,str[g]);
int temp=strlen(str[g]);
len2=temp;
if(KMP())continue;
else{
for(int m=;m<temp;m++)
s2[m]=str[g][temp-m-];
if(!KMP())break;
}
}
if(g>n){flag=true;break;}
}
if(flag){ans=i;break;} }
printf("%d\n",ans); }
}

Substrings的更多相关文章

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

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

  2. Leetcode: Unique Substrings in Wraparound String

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

  3. CSU-1632 Repeated Substrings (后缀数组)

    Description String analysis often arises in applications from biology and chemistry, such as the stu ...

  4. CF451D Count Good Substrings (DP)

    Codeforces Round #258 (Div. 2) Count Good Substrings D. Count Good Substrings time limit per test 2 ...

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

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

  6. 后缀数组---New Distinct Substrings

    Description Given a string, we need to find the total number of its distinct substrings. Input T- nu ...

  7. Codeforces Round #258 D Count Good Substrings --计数

    题意:由a和b构成的字符串,如果压缩后变成回文串就是Good字符串.问一个字符串有几个长度为偶数和奇数的Good字串. 分析:可知,因为只有a,b两个字母,所以压缩后肯定为..ababab..这种形式 ...

  8. SPOJ 694. Distinct Substrings (后缀数组不相同的子串的个数)转

    694. Distinct Substrings Problem code: DISUBSTR   Given a string, we need to find the total number o ...

  9. Codeforces Round #306 (Div. 2) A. Two Substrings 水题

    A. Two Substrings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/550/pro ...

  10. CF Two Substrings

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

随机推荐

  1. spring注解方式实现定时器,并且cron表达式中不识别L的方法

    1.Spring的配置: <beans xmlns:task="http://www.springframework.org/schema/task" xsi:schemaL ...

  2. SetupFactory +添加frm2.0工具

    网盘链接: http://pan.baidu.com/s/1c1DFRJM 带单独的添加frm2.0工具 原setupfactory文件下载地址:http://www.pc0359.cn/downin ...

  3. UIAlertView弹出框

    <Alert弹出框提示用户信息>    1.遵循代理方法<UIAlertViewDelete>    2.调用方法UIAlertView *alert = [[UIAlertV ...

  4. Oracle内存结构(之三)

    [深入解析--eygle] 学习笔记 1.2.3 PGA的管理: sys@felix SQL>show parameter area_size NAME TYPE VALUE --------- ...

  5. 【开源java游戏框架libgdx专题】-04-接口介绍及生命周期

    在核心项目中包含6大与操作系统交互的接口,每个后端都实现了这6大接口. Application:运行应用程序并通知API的客户端应用程序级别的事件,提供日志记录设施和查询方法,例如,内存使用. Fil ...

  6. jquery $('#btn').click与$("#btn").live("click",function()有什么区别?

    live方法绑定的事件处理函数,在页面中未来添加的元素只要满足原来的选择器,仍然会导致事件触发.普通的事件绑定则没有这个效果.对于#btn这个选择器来说,如果你未来将id为btn的元素删除,然后再创建 ...

  7. 解决Windows8前面板耳机无声的问题

    Windows8已经到来不久了,相信很多朋友已经在使用,在使用时也许会遇到前面板耳机无声的问题,网上的其他办法很麻烦还不一定能解决,在这里我会给大家提供最简单的办法解决这个问题. 百度经验:jingy ...

  8. jQuery选择器实现隔行变色

    <script type="text/javascript"> $(function(){ $("#tableName tr:nth-child(even)& ...

  9. 关于svn获取获取文件时 Unable to connect to a repository at URL"https://..."执行上下文错误:参数错误

    错误提示: 下面的六种解决方案都未能解决: 1.不提示输入用户名和密码,不管重装多少次都一样. 2.TortoiseSVN的setting->Saved Data->Authenticat ...

  10. 手动导出Excel及Excel导出原理探究

    前言:不知道大家有没碰到过,官方大型网站(例如中国移动)上很多地方查询列表是不提供数据导出的,而且界面上的筛选也无法满足我们的需求. 这时候我就想,如果能导出成Excel并借助自带的数据筛选方便多了. ...