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. DBCP,C3P0,Tomcat_JDBC 性能及稳定性测试

    1.测试环境: 硬件环境: 数据库服务器:2U*8核 8G内存 测试服务器:   2U*8核 6G内存 软件环境: jdk: 1.6.29 mysql: 5.0.77 mysql_driver: my ...

  2. solr5.3.1 集群服务搭建

    转http://978538.blog.51cto.com/968538/1710442 一. 安装部署 zookeeper集群部署: 节点: 10.1.12.51:2181      node1 1 ...

  3. NOI2003 文本编辑器

    练手QAQ #include<iostream> #include<algorithm> #include<cstdio> #include<cstdlib& ...

  4. 设置IIS7文件上传的最大大小 maxAllowedContentLength,maxRequestLength

    当上传一个超过30M的文件时,服务器会重定向至404.13页面,报错如下: HTTP Error 404.13 - Not Found The request filtering module is ...

  5. 阿基米德项目ALS矩阵分解算法应用案例

    转自:https://github.com/ceys/jdml/wiki/ALS 阿基米德项目ALS矩阵分解算法应用案例 编写人:ceys/youyis 最后更新时间:2014.5.12 一.算法描述 ...

  6. Android(java)学习笔记234: 服务(service)之音乐播放器

    1.我们播放音乐,希望在后台长期运行,不希望因为内存不足等等原因,从而导致被gc回收,音乐播放终止,所以我们这里使用服务Service创建一个音乐播放器. 2.创建一个音乐播放器项目(使用服务) (1 ...

  7. python摇骰子猜大小的小游戏

    #小游戏,摇筛子押大小的小游戏玩家初始有1000块钱,可以压大压小作为赌注 import random #定义摇筛子的函数: def roll_dice(number = 3,points = Non ...

  8. Python开发【第十篇】:CSS --无内容点击-不进去(一)

    Python开发[第十篇]:CSS  --无内容点击-不进去(一)

  9. access的时间相关的查询

    string sql = "select * from CONCURRENCY WHERE CONCURRENCY.DATE_FLAG BETWEEN  cdate('2013-11-1', ...

  10. 使用Android SDK Manager自动下载速度慢解决方法

    可以在SDK Manager 的更新界面,勾选下载项并去android-sdk-windows\temp文件家中查找文件名称例如:android-2.3.1_r02-linux.zip在前面加上链接h ...