题目来源:URAL 1684. Jack's Last Word

题意:输入a b 把b分成若干段 每一段都是a的前缀

思路:b为主串 然后用a匹配b 记录到b的i位置最大匹配的长度 然后切割 切割的时候要从后往前

假设a = abac b = abab 那么假设从前往后 首先覆盖了aba 然后b就不能覆盖了 从后往前就能够了 首先覆盖ab 下一次还是ab

由于已经记录了到i位置的最大匹配长度 依据长度从末尾倒退 每次倒退的时候仅仅要是最大的匹配的长度

由于假设在某一次的递推 记录的最大匹配的前缀是x 那么这次应该倒退到i-x

假设不倒退x 倒退小于x的字符y 而且x是能够倒退 剩下的是y-x必然能够倒退 那么一次解决即可了

  1. #include <cstdio>
  2. #include <cstring>
  3. const int maxn = 100010;
  4. char a[maxn], b[maxn];
  5. int f[maxn];
  6. int dp[maxn];
  7. char c[maxn*2];
  8. void get_fail(char* s)
  9. {
  10. f[0] = f[1] = 0;
  11. int n = strlen(s);
  12. for(int i = 1; i < n; i++)
  13. {
  14. int j = f[i];
  15. while(j && s[i] != s[j])
  16. j = f[j];
  17. if(s[i] == s[j])
  18. f[i+1] = j+1;
  19. else
  20. f[i+1] = 0;
  21. }
  22. }
  23. int main()
  24. {
  25. while(scanf("%s %s", a, b) != EOF)
  26. {
  27. get_fail(a);
  28. int n = strlen(b), m = strlen(a);
  29. int j = 0;
  30. for(int i = 0; i < n; i++)
  31. {
  32. while(j && b[i] != a[j])
  33. j = f[j];
  34. if(a[j] == b[i])
  35. j++;
  36. dp[i] = j;
  37. if(j == m)
  38. j = f[j];
  39.  
  40. }
  41. c[n*2] = 0;
  42. int len = n*2, i;
  43. for(i = n-1; i >= 0; )
  44. {
  45. int k = dp[i];
  46. if(k == 0)
  47. break;
  48.  
  49. for(int j = i; j > i-k; j--)
  50. c[--len] = a[j-i+k-1];
  51. c[--len] = ' ';
  52. i = i-k;
  53. }
  54. if(i != -1)
  55. puts("Yes");
  56. else
  57. {
  58. puts("No");
  59. puts(c+len+1);
  60. }
  61. }
  62. return 0;
  63. }

URAL 1684. Jack&#39;s Last Word KMP的更多相关文章

  1. URAL 1684. Jack's Last Word ( KMP next函数应用 )

    题意:问第二行的串能不能恰好分割成几个串,使得这几个串都是第一行串的前缀.如果是,输出No, 并输出这几个串,否则输出Yes. 这题是Special Judge,把两个串连接起来,中间用一个未出现过的 ...

  2. URAL 1707. Hypnotoad&#39;s Secret(树阵)

    URAL 1707. Hypnotoad's Secret space=1&num=1707" target="_blank" style="" ...

  3. URAL 1837. Isenbaev&#39;s Number (map + Dijkstra || BFS)

    1837. Isenbaev's Number Time limit: 0.5 second Memory limit: 64 MB Vladislav Isenbaev is a two-time ...

  4. ZOJ 3587 Marlon&#39;s String 扩展KMP

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3587 题意:给出两个字符串S和T.S,T<=100000.拿出 ...

  5. ZOJ 题目3587 Marlon&#39;s String(KMP)

    Marlon's String Time Limit: 2 Seconds      Memory Limit: 65536 KB Long long ago, there was a coder n ...

  6. URAL 1727. Znaika&#39;s Magic Numbers(数学 vector)

    主题链接:http://acm.timus.ru/problem.aspx?space=1&num=1727 1727. Znaika's Magic Numbers Time limit: ...

  7. ACM数据结构相关资料整理【未完成,待补充】

    在网上总是查不到很系统的练ACM需要学习的数据结构资料,于是参考看过的东西,自己整理了一份. 能力有限,欢迎大家指正补充. 分类主要参考<算法竞赛入门经典训练指南>(刘汝佳),山东大学数据 ...

  8. 中文分词工具探析(一):ICTCLAS (NLPIR)

    1. 前言 ICTCLAS是张华平在2000年推出的中文分词系统,于2009年更名为NLPIR.ICTCLAS是中文分词界元老级工具了,作者开放出了free版本的源代码(1.0整理版本在此). 作者在 ...

  9. TMS320C54x系列DSP的CPU与外设——第2章 TMS320C54x DSP体系结构总体介绍

    第2章 TMS320C54x DSP体系结构总体介绍 本章介绍TMS320C54x DSP体系结构的概况,包括中央处理单元(CPU).存在器和片内外设. C54x DSP采用了高级的改进哈佛结构,用8 ...

随机推荐

  1. JS学习笔记 - 面向对象 - 原型

    <script> var arr1 = new Array(12, 55, 34, 78, 676); var arr2 = new Array(12, 33, 1) Array.prot ...

  2. 【LCS】POJ1458Common Subsequence

    题目链接:http://poj.org/problem?id=1458 这是一道最长公共子序列的模板题: #include<iostream> #include<string> ...

  3. 读<阿里亿级日活网关通道架构演进>有感

    读<阿里亿级日活网关通道架构演进>时对优化方法有些概念不理解,特意搜索了一下,拓展自己的思路. 其中的优化: 优化方法中1,2比较常见,3,4我知道的比较少,很感兴趣.就继续追踪下去: 于 ...

  4. UVA 11136 - Hoax or what (可以提交了,不会Submission error了)

    看题传送门:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  5. 【Codeforces Round #440 (Div. 2) C】 Maximum splitting

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 肯定用尽量多的4最好. 然后对4取模的结果 为0,1,2,3分类讨论即可 [代码] #include <bits/stdc++ ...

  6. TreeView控件的展开与折叠

    在窗体中添加一个TreeView控件,设置CheckBox属性为True,绑定数据 Archive jkj = new Archive();//自定义类        public void Bind ...

  7. MongoDB 管理

    1.给数据库增加分片功能 mongos> use admin mongos> db.runCommand({enablesharding:"cipnet"}) mong ...

  8. 34、uevent机制说明

    class_device_create(4.3.2内核是device_create->device_create_vargs->device_register->device_add ...

  9. 【26.83%】【Codeforces Round #380C】Road to Cinema

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  10. [Node.js] Provide req.locals data though middleware

    We can create Template Helpers, which can contains some common reuseable data and libs. /* This is a ...