Substrings
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的更多相关文章
- [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- Leetcode: Unique Substrings in Wraparound String
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- CSU-1632 Repeated Substrings (后缀数组)
Description String analysis often arises in applications from biology and chemistry, such as the stu ...
- CF451D Count Good Substrings (DP)
Codeforces Round #258 (Div. 2) Count Good Substrings D. Count Good Substrings time limit per test 2 ...
- LA4671 K-neighbor substrings(FFT + 字符串Hash)
题目 Source http://acm.hust.edu.cn/vjudge/problem/19225 Description The Hamming distance between two s ...
- 后缀数组---New Distinct Substrings
Description Given a string, we need to find the total number of its distinct substrings. Input T- nu ...
- Codeforces Round #258 D Count Good Substrings --计数
题意:由a和b构成的字符串,如果压缩后变成回文串就是Good字符串.问一个字符串有几个长度为偶数和奇数的Good字串. 分析:可知,因为只有a,b两个字母,所以压缩后肯定为..ababab..这种形式 ...
- SPOJ 694. Distinct Substrings (后缀数组不相同的子串的个数)转
694. Distinct Substrings Problem code: DISUBSTR Given a string, we need to find the total number o ...
- 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 ...
- CF Two Substrings
Two Substrings time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
随机推荐
- URL重写:RewriteCond指令与RewriteRule 指令格式(转)
Rewirte主要的功能就是实现URL的跳转和隐藏真实地址,基于Perl语言的正则表达式规范.平时帮助我们实现拟静态,拟目录,域名跳转,防止盗链等.本文将针对mod_rewrite和URL匹配的技术细 ...
- qt 程序启动参数 -qws
运行嵌入式程序 在嵌入式QT版本中,程序需要服务器或自己作为服务器程序.服务器程序构造的方法是构造一个QApplication::GuiServe类型的QApplication对象.或者使用-qws命 ...
- [转] Git 分支 - 分支的新建与合并
http://git-scm.com/book/zh/v1/Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E ...
- Java基础知识强化之集合框架笔记09:Collection集合迭代器使用的问题探讨
1.Collection集合迭代器使用的问题探讨: (1)问题1:能用while循环写这个程序,我能不能用for循环呢? 可以使用for循环替代. (2)问题2:不要 ...
- MWEB+七牛 上传图片
MWEB+七牛 上传图片 博客之前的图片也都用的七牛,但编辑和上传分离还是很麻烦,所以一直很心水meb, 上周mweb降到¥50,感觉短期内应该不会再降了,于是果断入手,今天在和使用图床功能遇到了一些 ...
- CSS控制表单
一个简单的网站注册页面制作. 创建CSS文件如下: @charset "utf-8"; /* CSS Document */ * { margin: 0px; padding: 0 ...
- ScrollView、ListView嵌套
1.手动设置ListView高度 经过测试发现,在xml中直接指定ListView的高度,是可以解决这个问题的,但是ListView中的数据是可变的,实际高度还需要实际测量.于是手动代码设置ListV ...
- javaee后台适合用的编辑器插件
http://pan.baidu.com/s/1bn7D9sr 这个适合用在后台
- 使用OpenFileDialog实现图片上传
demo效果图:
- 关于IO学习的几个函数
这是最近学到的几个关于IO文件操作的几个小算法,今天总结出来. 1. 删除一个给定的目录,这上目录不为空目录,使用递归来实现 public void test04(File file) { File[ ...