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. winform combobox控件绑定 分类: WinForm 2014-04-17 14:34 118人阅读 评论(0) 收藏

    想要达到的效果:把数据库中的一列数据绑定到combobox控件中. 数据库表:T_Task//任务表 列名:Task_Name//名称 主键:Task_ID combobox控件名称:cbName 解 ...

  2. ASP.NETserver控件使用之Reportviewer 报表

    1.       Reportviewer 报表 1.1.       Reportviewer控件 注:本教程附2个事例: l  演练:在本地处理模式下将数据库数据源与 ReportViewer W ...

  3. STM8S 独立看门狗配置及使用

    //独立看门口的时钟来源 内部低速时钟 128khz 除以2 即64khz //选择 IWDG_Prescaler_128 //64/128 =0.5 khz 2ms周期 #define IWDG_5 ...

  4. 第一个 Python 程序 - Email Manager Demo

    看了一些基础的 Python 新手教程后,深深感觉到 Python 的简洁与强大,这是我的第一个 Python Demo.下面是完整代码与执行截图. 代码: # encoding: utf-8 ''' ...

  5. Python之路,Day17 - 分分钟做个BBS论坛

    Python之路,Day17 - 分分钟做个BBS论坛   本节内容: 项目:开发一个简单的BBS论坛 需求: 整体参考"抽屉新热榜" + "虎嗅网" 实现不同 ...

  6. Python之路,Day12 - 那就做个堡垒机吧

    Python之路,Day12 - 那就做个堡垒机吧   本节内容 项目实战:运维堡垒机开发 前景介绍 到目前为止,很多公司对堡垒机依然不太感冒,其实是没有充分认识到堡垒机在IT管理中的重要作用的,很多 ...

  7. Day10 - Python协程、异步IO、redis缓存、rabbitMQ队列

    Python之路,Day9 - 异步IO\数据库\队列\缓存   本节内容 Gevent协程 Select\Poll\Epoll异步IO与事件驱动 Python连接Mysql数据库操作 RabbitM ...

  8. c# hasvalue属性

    // 数据类型? 表示参数的值可以为null空,此时这个参数可调用属性hasvalue来判断,此参数是否有除了null以外的值;进而进行其它的工作 //必须要加?才可用hasvalue属性 priva ...

  9. excel数据导入到sqlserver中---------工作笔记

    调用页面: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sys ...

  10. SQL 插入查询的最大ID 号 进行批量

    INSERT INTO tbl_image_language ( code, docomo_cd, au_cd, softbank_cd ) SELECT DISTINCT((SELECT max(c ...