思路: 对于每个子串,求出 母串中 所有该子串 的 开始和结束位置,保存在 mark数组中,求完所有子串后,对mark数组按 结束位置排序,然后 用后一个的结束位置 减去 前一个的 开始 位置 再 减去 1,记录最大值
比如 aaaqwer  1  aaa 那么 最长为 aaqwer

用strstr判断子串是否存在于母串中。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. const int maxn = 1000005;
  7.  
  8. struct MARK {
  9. int begin, end;
  10. bool operator <(const MARK& cmp) const {
  11. return end < cmp.end;
  12. }
  13. } mark[maxn];
  14.  
  15. char s[maxn], t[1005][105];
  16. int n, cnt, next[105];
  17.  
  18. void work(const char str[], const char sub[]) {
  19. //memset(next, 0, sizeof(next));
  20. //get_next(sub, next);
  21. int exp = 0, from, len = strlen(sub);
  22. while (strstr(str + exp, sub) != NULL) {
  23. from = strstr(str + exp, sub) - str;
  24. mark[cnt].begin = from;
  25. mark[cnt].end = from + len - 1;
  26. cnt++;
  27. exp = from + len - 1;
  28. }
  29. }
  30.  
  31. int main() {
  32. while (scanf("%s", s) == 1) {
  33. scanf("%d", &n);
  34. for (int i = 0; i < n; i++)
  35. scanf("%s", t[i]);
  36. cnt = 0;
  37. for (int i = 0; i < n; i++) {
  38. work(s, t[i]);
  39. }
  40. mark[cnt].begin = mark[cnt].end = strlen(s);
  41. cnt++;
  42. sort(mark, mark + cnt);
  43. int ans = -1;
  44. for (int i = 0; i < cnt - 1; i++) {
  45. int len = mark[i + 1].end - mark[i].begin - 1;
  46. if (len > ans)
  47. ans = len;
  48. }
  49. printf("%d\n", ans == -1 ? strlen(s) : ans);
  50. }
  51. }

用KMP判断子串是否存在于母串中。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. const int maxn = 1000005;
  7.  
  8. struct MARK {
  9. int begin, end;
  10. bool operator <(const MARK& cmp) const {
  11. return end < cmp.end;
  12. }
  13. } mark[maxn];
  14.  
  15. char s[maxn], t[1005][105];
  16. int n, cnt, next[105];
  17.  
  18. void get_next(const char *sub, int *next) {
  19. int len = strlen(sub);
  20. int i, k;
  21. next[0] = k = -1;
  22. for (i = 0; i < len;) {
  23. if (k == -1 || sub[i] == sub[k]) {
  24. k++;
  25. i++;
  26. if (sub[k] != sub[i])
  27. next[i] = k;
  28. else
  29. next[i] = next[k];
  30. } else
  31. k = next[k];
  32. }
  33. }
  34.  
  35. int KMP(const char *str, const char *sub, const int *next) {
  36. int i, j;
  37. int len1 = strlen(str), len2 = strlen(sub);
  38. for (i = 0, j = 0; i < len1 && j < len2;) {
  39. if (j == -1 || str[i] == sub[j]) {
  40. i++;
  41. j++;
  42. } else
  43. j = next[j];
  44. }
  45. if (j == len2)
  46. return i - len2;
  47. return -1;
  48. }
  49.  
  50. void work(const char str[], const char sub[]) {
  51. //memset(next, 0, sizeof(next));
  52. get_next(sub, next);
  53. int exp = 0, from = 0, len = strlen(sub);
  54. while ((from = KMP(str + exp, sub, next)) != -1) {
  55. mark[cnt].begin = from + exp;
  56. mark[cnt].end = from + exp + len - 1;
  57. cnt++;
  58. exp += from + len - (len == 1 ? 0 : 1);
  59. }
  60. }
  61.  
  62. int main() {
  63. while (scanf("%s", s) == 1) {
  64. scanf("%d", &n);
  65. for (int i = 0; i < n; i++)
  66. scanf("%s", t[i]);
  67. cnt = 0;
  68. for (int i = 0; i < n; i++) {
  69. work(s, t[i]);
  70. }
  71. // for (int i = 0; i < cnt; i++)
  72. // printf("%d %d\n", mark[i].begin, mark[i].end);
  73. mark[cnt].begin = mark[cnt].end = strlen(s);
  74. cnt++;
  75. sort(mark, mark + cnt);
  76. int ans = -1;
  77. for (int i = 0; i < cnt - 1; i++) {
  78. int len = mark[i + 1].end - mark[i].begin - 1;
  79. if (len > ans)
  80. ans = len;
  81. }
  82. printf("%d\n", ans == -1 ? strlen(s) : ans);
  83. }
  84. }

福州大学第十届校赛 & fzu 2128最长子串的更多相关文章

  1. FZU 2128 最长子串

    题目链接:最长子串 思路:依次找出每个子串的在字符串中的首尾地址,所有子串先按照尾地址从小到大排序.然后首地址从小到大排. 遍历一遍每个子串的首地址和它后面相邻子串的尾地址之差-1, 第一个子串的首地 ...

  2. 河南省第十届省赛 Plumbing the depth of lake (模拟)

    title: Plumbing the depth of lake 河南省第十届省赛 题目描述: There is a mysterious lake in the north of Tibet. A ...

  3. 河南省第十届省赛 Intelligent Parking Building

    title: Intelligent Parking Building 河南省第十届省赛 tags: [模拟,省赛] 题目描述: There is a new revolution in the pa ...

  4. 四川第十届省赛 A.Angel Beats bitset

    四川第十届省赛 A.Angel Beats bitset 题目链接 题解参考:http://www.cnblogs.com/Aragaki/p/9142250.html 考虑用bitset来维护对于所 ...

  5. squee_spoon and his Cube VI---郑大校赛(求最长子串)

    市面上最常见的魔方,是三阶魔方,英文名为Rubik's Cube,以魔方的发明者鲁比克教授的名字命名.另外,二阶魔方叫Pocket Cube,它只有2*2*2个角块,通常也就比较小:四阶魔方叫Reve ...

  6. Problem 2128 最长子串(kmp+strstr好题经典)

     Problem 2128 最长子串 Accept: 134    Submit: 523Time Limit: 3000 mSec    Memory Limit : 65536 KB  Probl ...

  7. CSUST 第15届 校赛总结

    一直想记录一下自己的比赛,却感觉空间说说有点不适,思考了一番还是打算放到自己的博客园 这次比赛总体来说还是不错,签到还是稳的一批,基本前四小时都在rk1 开局切了几道签到题,然后开了一道思维gcd,正 ...

  8. HZNU第十二届校赛赛后补题

    愉快的校赛翻皮水! 题解 A 温暖的签到,注意用gets #include <map> #include <set> #include <ctime> #inclu ...

  9. fzu Problem 2128 最长子串(KMP + strstr 经典好题)

     Problem Description 问题很简单,给你一个字符串s,问s的子串中不包含s1,s2...sn的最长串有多长.  Input 输入包含多组数据.第一行为字符串s,字符串s的长度1到10 ...

随机推荐

  1. python 字符串处理

    介绍字符串相关的:比较,截取,替换,长度,连接,反转,编码,格式化,查找,复制,大小写,分割等操作 什么是字符串 字符串 字符串或串(String)是由数字.字母.下划线组成的一串字符.一般记为 s= ...

  2. Windows Service的官方描述,抄下来(不写obj就是LocalSystem)

    How to create a Windows service by using Sc.exe Email Print Support for Windows XP has ended Micro ...

  3. QScriptEngine

    其实你有好多没有介绍 比如qt文字 我一直很迷惑qt的文字的长宽 qt文字的字间距 等等这些东西还有QProcess QProcess可能是qt调用c#的唯一方法了QScript要比你想象的重要,一个 ...

  4. 【C++】第二章:Hello World!

    1.开发工具:Microsoft Visual C++ v6.0 2.通过IDE建立Hello World程序: 我们可以看到三个文件夹结构,分别是: Source Files(源文件). Heade ...

  5. <脱机手写汉字识别若干关键技术研究>

    脱机手写汉字识别若干关键技术研究 对于大字符集识别问题,一般采用模板匹配的算法,主要是因为该算法比较简单,识别速度快.但直接的模板匹配算法往往无法满足实际应用中对识别精度的需求.为此任俊玲编著的< ...

  6. css sprites 图片精灵自动生成 插件

    grunt-spritesmith https://www.npmjs.com/package/grunt-spritesmith

  7. android Bitmap(将视图转为bitmap对象)

    1)从android的资源文件夹layout中加载xml布局文件,并把布局文件映射为Bitmap main.xml文件如下: <?xmlversion="1.0"encodi ...

  8. MVC:Controller向View传值方式总结

    Controller向View传值方式总结 总结发现ASP.NET MVC中Controller向View传值的方式共有6种,分别是: ViewBag ViewData TempData 向普通Vie ...

  9. NotePad++ 快捷键中文说明

    Ctrl-H 打开Find / Replace 对话框 Ctrl-D 复制当前行 Ctrl-L 删除当前行 Ctrl-T 上下行交换 F3 找下一个 Shift-F3 找上一个 Ctrl-Shift- ...

  10. 来自中油瑞飞的SQL笔试题20131202

    1.有三张表,用户表,用户角色表,角色表, 使用sql显示如下内容: 用户ID,用户名,超级管理员,录入员,会计 也就是角色用逗号分隔. 解: 1.填充数据到表User select * from [ ...