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

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

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

  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<cstring>
  4. #define N 205
  5. using namespace std;
  6. int next[N];
  7. int n;
  8. char s1[N];//模板串
  9. char s2[N];//主串
  10. int len1,len2,ans;
  11. char str[][N];
  12. void getnext(int plen){
  13. int i = ,j = -;
  14. next[]=-;
  15. while( i<plen ){
  16. if(j==-||s1[i]==s1[j]){
  17. ++i;++j;
  18. if(s1[i]!=s1[j] )
  19. next[i] = j;
  20. else
  21. next[i] = next[j];
  22. }
  23. else
  24. j = next[j];
  25. }
  26. }
  27. bool KMP(){
  28. int i = ,j = ;
  29. while (i<len2&&j<len1){
  30. if( j == - || s2[i] == s1[j] ){
  31. ++i;
  32. ++j;
  33. }
  34. else {
  35. j = next[j];
  36. }
  37. }
  38. if(j>=len1)return true;
  39. else
  40. return false;
  41. }
  42. int main(){
  43. int cas;
  44. scanf("%d",&cas);
  45. while(cas--){
  46. scanf("%d",&n);
  47. int pos=-,minn=,ans=;
  48. bool flag=false;
  49. for(int i=;i<=n;i++){
  50. scanf("%s",&str[i]);
  51. int len=strlen(str[i]);
  52. if(len<minn){
  53. minn=len;pos=i;
  54. }
  55. }
  56. for(int i=minn;i>=;i--){
  57. for(int j=;j+i<=minn;j++){
  58. memset(s1,,sizeof(s1));
  59. for(int k=j;k<j+i;k++){
  60. s1[k-j]=str[pos][k];
  61. }
  62. // printf("**%s\n",s1);
  63. len1=i;
  64. getnext(i);
  65. int g;
  66. for(g=;g<=n;g++){
  67. strcpy(s2,str[g]);
  68. int temp=strlen(str[g]);
  69. len2=temp;
  70. if(KMP())continue;
  71. else{
  72. for(int m=;m<temp;m++)
  73. s2[m]=str[g][temp-m-];
  74. if(!KMP())break;
  75. }
  76. }
  77. if(g>n){flag=true;break;}
  78. }
  79. if(flag){ans=i;break;}
  80.  
  81. }
  82. printf("%d\n",ans);
  83.  
  84. }
  85. }

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. angular ng-class使用笔记

    在前面Angularjs开发一些经验总结中说到在angular开发中angular controller never 包含DOM元素(html/css),在controller需要一个简单的POJO( ...

  2. GNU Make----Core Automatic Variables

    $@ 表示规则的目标文件名.如果目标是一个文档文件(Linux中,一般称.a 文件为 文档文件,也称为静态库文件),那么它代表这个文档的文件名.在多目标模式 规则中,它代表的是哪个触发规则被执行的目标 ...

  3. 手把手教你从 Core Data 迁移到 Realm

    前言 看了这篇文章的标题,也许有些人还不知道Realm是什么,那么我先简单介绍一下这个新生的数据库.号称是用来替代SQLite 和 Core Data的.Realm有以下优点: 使用方便 Realm并 ...

  4. uva 11324 The Largest Clique (Tarjan+记忆化)

    /*每个环 要么不选 要么全选 可缩点 就得到一个GAD图 然后搞搞算出最大路径*/ #include<iostream> #include<cstdio> #include& ...

  5. 排名最重要的三个优化阶段分析 --------------------->>转至(卧牛SEO/武汉SEO http://blog.sina.com.cn/zhengkangseo )

    网站排名,不是一两天能够决定的.要想取得好的排名,得分时间分阶段地做排名,网站优化分前期,中期,后期,怎么来区别不同的阶段该用怎样的优化手段.今晚SEO研究中心创始人Moon老师分享:排名最重要的三个 ...

  6. android - startActivity浅谈

    当执行startActivity(Intent intent, Bundle options)函数的时候,应用程序不是直接呼叫另外一个Activity,而是将intent传进Android框架中.An ...

  7. Binary XML file : Error inflating class com.esri.android.map.MapView

    在测试esri arcgis for android的第一个程序Helloworld的时候,报这样的错: Binary XML file : Error inflating class com.esr ...

  8. CRT detected that the application wrote to memory after end of heap buffer.

    很多人的解释都不一样,  我碰到的问题是,开辟的内存空间小于操作的内存空间.也就是说,我free的内存越界了. 这是我开辟链表结构体内存的代码: PNODE Create() { int len; / ...

  9. 解决 asp.net 伪静态 IIS设置后 直正HTML无法显示的问题

    asp.net+UrlRewriter来实现网站伪静态,实现伪静态有一些好处,比如对于搜索引擎更好收录页面,还有一个好处就是隐藏了真实地址的参数,所以有独立服务器的朋友,配置IIS实现伪静态功能,挺不 ...

  10. 1.Weblogic通Eclipse调试配置(Weblogic同Eclipse调试配置技术)

    概述:环境是eclipse,maven,svn, 在实际的的应用项目中,我们经常遇到本地应用程序没有问题,而部署到Weblogic上缺出现问题,查看日志并找不到原因,这时就需要调试部署上的程序与本地e ...