1、传送门:http://begin.lydsy.com/JudgeOnline/problem.php?id=2725

  题目大意:找一个串在另一个串中出现的次数

  题解:kmp(纯裸题)

  

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #define maxn 1000100
  5. int n,fix,ans,i,lens,lent;
  6. char s[maxn],t[maxn];
  7. int next[maxn];
  8. void getnext()
  9. {
  10. fix=;
  11. for (i=; i<=lent; i++)
  12. {
  13. while(fix && t[fix+]!=t[i]) fix=next[fix];
  14. if (t[fix+]==t[i]) fix++;
  15. next[i]=fix;
  16. }
  17. }
  18. int KMP()
  19. {
  20. int count;
  21. fix=; count=;
  22. for (int i=; i<=lens; i++)
  23. {
  24. while (fix && t[fix+]!=s[i]) fix=next[fix];
  25. if (t[fix+]==s[i]) fix++;
  26. if (fix==lent)
  27. {
  28. count++;
  29. fix=next[fix];
  30. }
  31. }
  32. return count;
  33. }
  34. int main()
  35. {
  36. int z;
  37. scanf("%d",&z);
  38. for (int zz=; zz<=z; zz++)
  39. {
  40. scanf("%s",t+);
  41. scanf("%s",s+);
  42. lens=strlen(s+);
  43. lent=strlen(t+);
  44. memset(next,,sizeof(next));
  45. getnext();
  46. ans=KMP();
  47. printf("%d\n",ans);
  48. }
  49. return ;
  50. }

  

2、传送门:http://begin.lydsy.com/JudgeOnline/problem.php?id=2732

  题目大意:给你一个字符串,让你求出最大重复周期(最大周期不和本身重合)

  题解:kmp或者扩展kmp(但我并未用这种方法),我用的是kmp,但是一直WA,原来是求next数组把while写成了if(手抖毁一生)。

     好吧,写题解了:用kmp求出next数组,然后在去递归next[n],因为j=next[next[n]]一直next下去直到其的next为0就ans+=n-j;

     这就可以求出不和母串一样的最大重复周期,但是这又有一个问题你在求j时要递归时间就有可能为n^2的所以在每次递归时改变next的值就好了(详见代码);

  

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #define inf 0x7ffffff
  5. #define maxn 1000100
  6. char s[maxn];
  7. int nnext[maxn];
  8. int n,i,j,fix;
  9. long long ans;
  10. using namespace std;
  11. int main()
  12. {
  13. scanf("%d",&n);
  14. scanf("%s",s+);
  15. fix=;
  16. for (int i=; i<=n; i++)
  17. {
  18. while (fix && s[fix+]!=s[i]) fix=nnext[fix];
  19. if (s[fix+]==s[i]) fix++;
  20. nnext[i]=fix;
  21. }
  22. ans=;
  23. for (int i=; i<=n; i++)
  24. {
  25. int j=i;
  26. if (!nnext[j]) continue;
  27. while (nnext[nnext[j]]) nnext[j]=nnext[nnext[j]];
  28. ans+=(i-nnext[j]);
  29. }
  30. printf("%lld\n",ans);
  31. }

3、传送门:http://begin.lydsy.com/JudgeOnline/problem.php?id=2726

  题目大意:给出一个字母组成的矩阵,找出一个最小的子矩阵,使得这个子矩阵的无限复制扩张之后的矩阵包含原来矩阵如:         

        ABABA

        ABABA 
        他的最小重复子矩阵是AB

  题解:还是kmp,只不过要有一点小技巧,就是把一行当作一个元素,那么就有n个元素,然后做kmp找重复子串,那么最小重复子串就为n-next[n],列也做此操作,答案就是

      (n-r[n])*(m-c[m]);

  

  1. #include <cstdio>
  2. #include <cstring>
  3. char s[][], rs[][];
  4. int R[], C[];
  5. int r, c;
  6.  
  7. void get_nextR()
  8. {
  9. R[] = -;
  10. int j = -, i = ;
  11. while(i < r)
  12. {
  13. if(j == - || strcmp(s[i], s[j]) == )
  14. {
  15. i++;
  16. j++;
  17. R[i] = j;
  18. }
  19. else
  20. j = R[j];
  21. }
  22. }
  23.  
  24. void get_nextC()
  25. {
  26. C[] = -;
  27. int j = -, i = ;
  28. while(i < c)
  29. {
  30. if(j == - || strcmp(rs[i], rs[j]) == )
  31. {
  32. i++;
  33. j++;
  34. C[i] = j;
  35. }
  36. else
  37. j = C[j];
  38. }
  39. }
  40.  
  41. int main()
  42. {
  43. while(scanf("%d %d", &r, &c) != EOF)
  44. {
  45. for(int i = ; i < r; i++)
  46. scanf("%s", s[i]);
  47. get_nextR();
  48. for(int i = ; i < r; i++)
  49. for(int j = ; j < c; j++)
  50. rs[j][i] = s[i][j];
  51. get_nextC();
  52. printf("%d\n", (r - R[r]) * (c - C[c]));
  53. }
  54. }

4、传送门:http://begin.lydsy.com/JudgeOnline/problem.php?id=2724

  题目大意:给定一个字符串,要求找到同时是它前缀也是后缀的字符串的个数,并且输出他们的长度。

  题解:理解一下next数组的用法,从next[n]一直往前求next,那么那些点的坐标就是answer。

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #define maxn 1000100
  5. int n,ans,fix;
  6. int nnext[maxn];
  7. char s[maxn];
  8. using namespace std;
  9. void show(int n)
  10. {
  11. if (n<=) return;
  12. show(nnext[n]);
  13. printf("%d ",n);
  14. }
  15. int main()
  16. {
  17. scanf("%d",&n);
  18. scanf("%s",s+);
  19. fix=;
  20. for (int i=; i<=n; i++)
  21. {
  22. while (fix && s[fix+]!=s[i]) fix=nnext[fix];
  23. if (s[fix+]==s[i]) fix++;
  24. nnext[i]=fix;
  25. }
  26. show(n);
  27. return ;
  28. }

5、传送门:http://begin.lydsy.com/JudgeOnline/problem.php?id=2723

 题目大意:

    现在给一个字符串A,和另一个字符串B。要你每次从B串中从左到右第第一个A串。 
    并且从B串中删除它,直到A串不为B串的子串。问需要几次删除操作。

  题解:next数组的应用,只不过用一个堆记录那些没匹配成功的字符,在用它进行匹配。

  1. #include<cstring>
  2. #include<cstdio>
  3. #include<cstring>
  4. char t[],s[];
  5. int m,n,top,i,fix,ans;
  6. int z[],next[],f[];
  7. using namespace std;
  8. int main()
  9. {
  10. scanf("%s%s",t+,s+);
  11. m=strlen(s+); n=strlen(t+);
  12. for (fix=,i=; i<=n; i++)
  13. {
  14. while (fix && t[fix+]!=t[i]) fix=next[fix];
  15. if (t[fix+]==t[i]) fix++;
  16. next[i]=fix;
  17. }
  18. for (int i=; i<=m; i++)
  19. {
  20. fix=f[z[top]];
  21. while (fix && t[fix+]!=s[i]) fix=next[fix];
  22. if (t[fix+]==s[i]) fix++;
  23.  
  24. if (fix==n) top-=(n-),ans++;
  25. else
  26. f[i]=fix, z[++top]=i;
  27. }
  28. printf("%d\n",ans);
  29. }

6、传送门:http://begin.lydsy.com/JudgeOnline/problem.php?id=2719

  题目大意:给出一个串S,它包含 N 个字符。设 Pi = S [ 1 .. I ] ,对于一些 Pi , 如果Pi能表示成K个字符串相连而成的(K > 1 ,且K尽量大),则打印I和K。

  题解:如果i mod(i-next[i]) =0 && (i/(i-next[i])>1 输出答案(i);

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #define inf 0x7fffffff
  5. int n,ans;
  6. int nnext[];
  7. char s[];
  8. using namespace std;
  9. int main()
  10. {
  11. int z=;
  12. while (true)
  13. {
  14. scanf("%d",&n);
  15. if (n==) break;
  16. z++;
  17. printf("Test case #%d\n",z);
  18. scanf("%s",s+);
  19. ans=;
  20. int fix=;
  21. for (int i=; i<=n; i++)
  22. {
  23. while (fix && s[fix+]!=s[i]) fix=nnext[fix];
  24. if (s[fix+]==s[i]) fix++;
  25. nnext[i]=fix;
  26. }
  27. for (int i=; i<=n; i++)
  28. {
  29. int k=i-nnext[i];
  30. if (i % k== && i/k>) printf("%d %d\n",i,i/k);
  31. }
  32. printf("\n");
  33. }
  34. }

7、传送门:http://begin.lydsy.com/JudgeOnline/problem.php?id=2720

  题目大意:给你一个字符串,它是由某个字符串不断自我连接形成的。 但是这个字符串是不确定的,现在只想知道它的最短长度是多少.

  题解:同上;

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #define inf 0x7fffffff
  5. int n,ans;
  6. int nnext[];
  7. char s[];
  8. using namespace std;
  9. int main()
  10. {
  11. scanf("%d",&n);
  12. scanf("%s",s+);
  13. ans=;
  14. int fix=;
  15. for (int i=; i<=n; i++)
  16. {
  17. while (fix && s[fix+]!=s[i]) fix=nnext[fix];
  18. if (s[fix+]==s[i]) fix++;
  19. nnext[i]=fix;
  20. }
  21. printf("%d\n",n-nnext[n]);
  22. }

2016——3——16 kmp 7题的更多相关文章

  1. mysql查询练习题-2016.12.16

    >>>>>>>>>> 练习时间:2016.12.16 编辑时间:2016-12-20-->22:12:08 题: 涉及:多表查询.ex ...

  2. 2016.8.16上午纪中初中部NOIP普及组比赛

    2016.8.16上午纪中初中部NOIP普及组比赛 链接:https://jzoj.net/junior/#contest/home/1334 这次也翻车了,感觉比之前难多了. 辛辛苦苦改完了,太难改 ...

  3. zstu.4194: 字符串匹配(kmp入门题&& 心得)

    4194: 字符串匹配 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 206  Solved: 78 Description 给你两个字符串A,B,请 ...

  4. 【转载】webstorm11(注册,激活,破解,码,一起支持正版,最新可用)(2016.11.16更新)

    很多人都发现 http://idea.lanyus.com/ 不能激活了 很多帖子说的 http://15.idea.lanyus.com/ 之类都用不了了 最近封的厉害仅作测试 选择 License ...

  5. HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. 51Nod 1277 字符串中的最大值(KMP,裸题)

    1277 字符串中的最大值 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 一个字符串的前缀是指包含该字符第一个字母的连续子串,例如: ...

  7. poj-2406(kmp水题)

    题意:定义一个a*b=字符串a连接字符串b:给你一个字符串s,问你这个字符串最多能用多少个字符串t连接得到:例如:aaaa=4个a构成: 解题思路:kmp水题,next数组除了查找字串以外最广泛的一种 ...

  8. HDU 1711 - Number Sequence - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

  9. POJ 3167 Cow Pattern ★(KMP好题)

    题意 给你一个数字序列S,再给一个数字序列pattern,S和pattern中的数字都是1到s(s<=25).每个序列里的数字都有个排名,也就是第几小,现在我们要用pattern来匹配S.在本题 ...

随机推荐

  1. 把Wordpress集成到zen-cart里方法 各种修改 经典机制

    作者: 闻庭牛 | 分类: zen cart插件精解 | 浏览: 4 | 评论: 暂时没有评论 如果你的Zen-cart需要一个Blog来发布一些你的最新动态,可以试试Wordpress,并且用WOZ ...

  2. PAT1027

    People in Mars represent the colors in their computers in a similar way as the Earth people. 火星人在他们的 ...

  3. hdu_3564_Another LIS(线段树+LIS)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3564 题意:给你N个数的位置.数i的位置为第i个数,比如 0 0 2,表示1插在第0个位置,此时数列为 ...

  4. 提升html5的性能体验系列之一避免切页白屏

    窗体切换白屏的现实问题 HTML5的性能比原生差很多,比如切页时白屏.列表滚动不流畅.下拉刷新和上拉翻页卡顿.在低端Android手机上,很多原生App常用的功能和体验效果都很难使用HTML5技术模拟 ...

  5. mvc ChildActionOnly + ActionName的用法

    ChildActionOnly的目的主要就是让这个Action不通过直接在地址栏输入地址来访问,而是需要通过RenderAction来调用它. <a href="javascript: ...

  6. Android编程之SparseArray<E>详解

    最近编程时,发现一个针对HashMap<Integer, E>的一个提示: 翻译过来就是:用SparseArray<E>来代替会有更好性能.那我们就来看看源码中SparseAr ...

  7. IIS判断W3WP进程对应哪个网站

    IIS 6 (Win2003 )中查看某个应用程序池对应那个 W3WP.exe 进程,可以使用如下命令,输出结果类似如下: C:\WINDOWS\system32>cscript iisapp. ...

  8. hql 链接查询

    第一部分.连接查询 一.内连接 内连接查询操作列出与连接条件匹配的数据行,它使用比较运算符比较被连接列的列值.内连接分三种: 1.等值连接:在连接条件中使用等于号(=)运算符比较被连接列的列值,其查询 ...

  9. VMware克隆CentOS虚拟机后固定IP的问题

    由于克隆虚拟机,VMware只是修改了虚拟机的名字等信息,并没有修改虚拟硬盘中的任何信息,导致克隆后网卡的MAC地址和操作系统中记录的mac地址不符,导致eth0启动不起来.操作系统记录了一个新网卡的 ...

  10. 在Window平台下安装xgboost的Python版本

    原文:http://blog.csdn.net/pengyulong/article/details/50515916 原文修改了两个地方才安装成功,第3步可以不用,第2步重新生成所有的就行了. 第4 ...