codeforces 432D Prefixes and Suffixes
由于包含了前缀与后缀,很容易想到用KMP去算前缀与后缀的公共缀。另外要计算某个后缀在整个串中出现的次数,由于后缀自动机是比较容易求的,然后就直接上后缀自动机了。先分别用KMP算法与后缀自动机跑一遍,然后对后缀自动机做一个拓扑排序,根据拓扑结构更新一遍每个串出现的次数就可以了。然后直接沿着KMP的next数组以及后缀自动机节点的父亲边走就可以了。
代码如下:
#include <stdio.h>
#include <string.h>
const int maxn = ; struct sanode{
sanode *f, *ch[];
int l, m;
sanode(){
f = ;
memset(ch, , sizeof(ch));
l = , m = ;
}
void init(){
f = ;
memset(ch, , sizeof(ch));
l = , m = ;
}
}sam[maxn*], *b[maxn*];
int cnt[maxn], tot;
sanode *tail, *s;
char str[maxn];
int f[maxn]; //KMP
struct resultSet{
int l, m;
}res[maxn]; void addSuffix(int c, int len){
sanode *p = tail, *np = &sam[++tot];
np->init();
tail = np;
np->l = len; for(;p&&!p->ch[c];p=p->f) p->ch[c] = np;
if(!p) np->f = s;
else{
if(p->ch[c]->l == p->l + ) np->f = p->ch[c];
else{
sanode * q = p->ch[c], *r = &sam[++tot];
*r = *q;
r->l = p->l + ;
r->m = ;
q->f = np->f = r;
for(;p && p->ch[c]==q; p=p->f) p->ch[c] = r;
}
}
} void topSortSuffix(int len){
for(int i = ; i <= tot; i ++) cnt[sam[i].l] ++;
for(int i = ; i <= len; i ++) cnt[i] += cnt[i-];
for(int i = ; i <= tot; i ++) b[--cnt[sam[i].l]] = &sam[i]; for(int i = tot; i > ; i --){
b[i]->f->m += b[i]->m;
}
}
void KMP(){
int c = ;
for(int i = ; str[i]; i ++){
while(c && str[c+] != str[i])
c = f[c];
if(str[c+] == str[i])
f[i] = ++c;
}
} int main(){
int len = ;
s = tail = &sam[tot = ];
s->init();
scanf("%s", str+);
for(len = ; str[len]; len ++){
addSuffix(str[len] - 'A', len);
}
len --;
topSortSuffix(len);
KMP();
sanode *p = tail;
int number = ;
for(; len; len = f[len]){
while(p && p->l > len)
p = p->f;
if(!p) break;
if(p->l == len){
res[number].l = len;
res[number].m = p->m;
number ++;
}
}
printf("%d\n", number);
for(int i = number - ; i >= ; i --)
printf("%d %d\n", res[i].l , res[i].m); return ;
}
codeforces 432D Prefixes and Suffixes的更多相关文章
- Codeforces 432D Prefixes and Suffixes(KMP+dp)
题目连接:Codeforces 432D Prefixes and Suffixes 题目大意:给出一个字符串,求全部既是前缀串又是后缀串的字符串出现了几次. 解题思路:依据性质能够依据KMP算法求出 ...
- Codeforces 432D Prefixes and Suffixes kmp
手动转田神的大作:http://blog.csdn.net/tc_to_top/article/details/38793973 D. Prefixes and Suffixes time limit ...
- codeforces - 432D Prefixes and Suffixes (next数组)
http://codeforces.com/problemset/problem/432/D 转自:https://blog.csdn.net/tc_to_top/article/details/38 ...
- Codeforces 432D Prefixes and Suffixes:KMP + dp
题目链接:http://codeforces.com/problemset/problem/432/D 题意: 给你一个字符串s,让你找出所有既是前缀又是后缀的子串,并输出它们分别出现了多少次. 题解 ...
- Codeforces 432D Prefixes and Suffixes (KMP、后缀数组)
题目链接: https://codeforces.com/contest/432/problem/D 题解: 做法一: KMP 显然next树上\(n\)的所有祖先都是答案,出现次数为next树子树大 ...
- Codeforces 1092C Prefixes and Suffixes(思维)
题目链接:Prefixes and Suffixes 题意:给定未知字符串长度n,给出2n-2个字符串,其中n-1个为未知字符串的前缀(n-1个字符串长度从1到n-1),另外n-1个为未知字符串的后缀 ...
- 432D Prefixes and Suffixes
题目大意 给你一个串 对于一个子串如果它既是前缀又是后缀 输出它的长度以及它在原串中一共出现了多少次 分析 对于既是前缀又是后缀的判断和126B相同 然后我们只需要记录每个不同的z[i]出现了多少次 ...
- CodeForces Round #527 (Div3) C. Prefixes and Suffixes
http://codeforces.com/contest/1092/problem/C Ivan wants to play a game with you. He picked some stri ...
- Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes
D. Prefixes and Suffixes You have a string s = s ...
随机推荐
- MVC-登录并设置角色
1.新建一个类,设置角色: using System; using System.Collections.Generic; using System.Linq; using System.Text; ...
- 关于 hashCode() 你需要了解的 3 件事
(点击上方公众号,可快速关注) 原文:eclipsesource 译文:ImportNew - 南半球 链接:http://www.importnew.com/16517.html 在 Java 中, ...
- linux下定时发送邮件
at命令可以在某个时间运行某个程序,而mail可以以命令行的方式把存于一个文本中的邮件正文发送抄送出去. 具体用法: 1. 把email正文准备好,比如写在email.txt里 2. 然后写一个脚 ...
- UIWebView 加载网页、文件、 html-b
UIWebView 是用来加载加载网页数据的一个框.UIWebView可以用来加载pdf word doc 等等文件 生成webview 有两种方法,1.通过storyboard 拖拽 2.通过al ...
- mvc3.0中[ValidateInput(false)]失效的问题
在asp.net mvc3.0中[ValidateInput(false)]特性失效了,只需要在网站根目录中的web.config中做如下配置即可: <system.web> <ht ...
- 事件——《JS高级程序设计》
一. 事件流 1. 事件流描述的是从页面中接收事件的顺序 2. 事件冒泡(event bubble):事件从开始时由最具体的元素(就是嵌套最深的那个节点)开始,逐级向上传播到较为不具体的节点(就是Do ...
- BZOJ 3983 Takeover Wars 解题报告
我猜了一个结论,能合并就合并,到了必须要敌对交易的时候才进行敌对交易. 然后合并的话,肯定是拿最大的两个去合并. 至于敌对交易,肯定是干掉对方最大的公司才是有意义的. 于是各种分类讨论...看代码好了 ...
- ANGULAR 2 FOR REACT DEVELOPERS
Now that Angular 2 is in beta, the time has come for us to drop everything and learn something new, ...
- cxf2.4.3中jaxb-api.jar、jaxws-api.jar与jdk1.6.0_02不兼容问题
http://chxiaowu.iteye.com/blog/1243475 Exception in thread "main" java.lang.NoClassDefFoun ...
- maven 项目编译时候提示:Error building POM (may not be this project's POM).
编译时候提示Error building POM (may not be this project's POM)的错误,具体信息如下: [0] 'dependencies.dependency.ver ...