URAL 1684. Jack's Last Word KMP
题目来源: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必然能够倒退 那么一次解决即可了
- #include <cstdio>
- #include <cstring>
- const int maxn = 100010;
- char a[maxn], b[maxn];
- int f[maxn];
- int dp[maxn];
- char c[maxn*2];
- void get_fail(char* s)
- {
- f[0] = f[1] = 0;
- int n = strlen(s);
- for(int i = 1; i < n; i++)
- {
- int j = f[i];
- while(j && s[i] != s[j])
- j = f[j];
- if(s[i] == s[j])
- f[i+1] = j+1;
- else
- f[i+1] = 0;
- }
- }
- int main()
- {
- while(scanf("%s %s", a, b) != EOF)
- {
- get_fail(a);
- int n = strlen(b), m = strlen(a);
- int j = 0;
- for(int i = 0; i < n; i++)
- {
- while(j && b[i] != a[j])
- j = f[j];
- if(a[j] == b[i])
- j++;
- dp[i] = j;
- if(j == m)
- j = f[j];
- }
- c[n*2] = 0;
- int len = n*2, i;
- for(i = n-1; i >= 0; )
- {
- int k = dp[i];
- if(k == 0)
- break;
- for(int j = i; j > i-k; j--)
- c[--len] = a[j-i+k-1];
- c[--len] = ' ';
- i = i-k;
- }
- if(i != -1)
- puts("Yes");
- else
- {
- puts("No");
- puts(c+len+1);
- }
- }
- return 0;
- }
URAL 1684. Jack's Last Word KMP的更多相关文章
- URAL 1684. Jack's Last Word ( KMP next函数应用 )
题意:问第二行的串能不能恰好分割成几个串,使得这几个串都是第一行串的前缀.如果是,输出No, 并输出这几个串,否则输出Yes. 这题是Special Judge,把两个串连接起来,中间用一个未出现过的 ...
- URAL 1707. Hypnotoad's Secret(树阵)
URAL 1707. Hypnotoad's Secret space=1&num=1707" target="_blank" style="" ...
- URAL 1837. Isenbaev's Number (map + Dijkstra || BFS)
1837. Isenbaev's Number Time limit: 0.5 second Memory limit: 64 MB Vladislav Isenbaev is a two-time ...
- ZOJ 3587 Marlon's String 扩展KMP
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3587 题意:给出两个字符串S和T.S,T<=100000.拿出 ...
- ZOJ 题目3587 Marlon's String(KMP)
Marlon's String Time Limit: 2 Seconds Memory Limit: 65536 KB Long long ago, there was a coder n ...
- URAL 1727. Znaika's Magic Numbers(数学 vector)
主题链接:http://acm.timus.ru/problem.aspx?space=1&num=1727 1727. Znaika's Magic Numbers Time limit: ...
- ACM数据结构相关资料整理【未完成,待补充】
在网上总是查不到很系统的练ACM需要学习的数据结构资料,于是参考看过的东西,自己整理了一份. 能力有限,欢迎大家指正补充. 分类主要参考<算法竞赛入门经典训练指南>(刘汝佳),山东大学数据 ...
- 中文分词工具探析(一):ICTCLAS (NLPIR)
1. 前言 ICTCLAS是张华平在2000年推出的中文分词系统,于2009年更名为NLPIR.ICTCLAS是中文分词界元老级工具了,作者开放出了free版本的源代码(1.0整理版本在此). 作者在 ...
- TMS320C54x系列DSP的CPU与外设——第2章 TMS320C54x DSP体系结构总体介绍
第2章 TMS320C54x DSP体系结构总体介绍 本章介绍TMS320C54x DSP体系结构的概况,包括中央处理单元(CPU).存在器和片内外设. C54x DSP采用了高级的改进哈佛结构,用8 ...
随机推荐
- JS学习笔记 - 面向对象 - 原型
<script> var arr1 = new Array(12, 55, 34, 78, 676); var arr2 = new Array(12, 33, 1) Array.prot ...
- 【LCS】POJ1458Common Subsequence
题目链接:http://poj.org/problem?id=1458 这是一道最长公共子序列的模板题: #include<iostream> #include<string> ...
- 读<阿里亿级日活网关通道架构演进>有感
读<阿里亿级日活网关通道架构演进>时对优化方法有些概念不理解,特意搜索了一下,拓展自己的思路. 其中的优化: 优化方法中1,2比较常见,3,4我知道的比较少,很感兴趣.就继续追踪下去: 于 ...
- UVA 11136 - Hoax or what (可以提交了,不会Submission error了)
看题传送门:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- 【Codeforces Round #440 (Div. 2) C】 Maximum splitting
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 肯定用尽量多的4最好. 然后对4取模的结果 为0,1,2,3分类讨论即可 [代码] #include <bits/stdc++ ...
- TreeView控件的展开与折叠
在窗体中添加一个TreeView控件,设置CheckBox属性为True,绑定数据 Archive jkj = new Archive();//自定义类 public void Bind ...
- MongoDB 管理
1.给数据库增加分片功能 mongos> use admin mongos> db.runCommand({enablesharding:"cipnet"}) mong ...
- 34、uevent机制说明
class_device_create(4.3.2内核是device_create->device_create_vargs->device_register->device_add ...
- 【26.83%】【Codeforces Round #380C】Road to Cinema
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- [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 ...