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 ...
随机推荐
- 【toplink】 位居第一的Java对象关系可持续性体系结构
TopLink,是位居第一的Java对象关系可持续性体系结构,原署WebGain公司的产品,后被Oracle收购,并重新包装为Oracle AS TopLink.TOPLink为在关系数据库表中存储 ...
- MVC-ActionResult解说
HttpNotFoundResult: 专门用来响应Http404找不到网页的错误,在System.Web.Mvc.Controller类别中内建了一个HttpNotFound()方法,可以很方便的回 ...
- python与编码
Python中的文字对象 Python 3.x中处理文字的对象有str, bytes, bytearray. bytes和bytearray可以使用除了用作格式化的方法(format, format_ ...
- oracle学习之-----操作表中的数据
1. 向表中添加数据(Insert 语句): 添加的语法: INSERT INTO table_name(column1,column2,column3,......) VALUES(value1,v ...
- 百度:在O(1)空间复杂度范围内对一个数组中前后连段有序数组进行归并排序
一.题目理解 题目:数组al[0,mid-1]和al[mid,num-1]是各自有序的,对数组al[0,num-1]的两个子有序段进行merge,得到al[0,num-1]整体有序.要求空间复杂度为O ...
- BT5之配置笔记
BT5本来就是用Ubuntu 10.04做得蓝本,所以,我在配置BT5的时候,基本上都是按照Ubuntu 10.04的配置方法,在配置BT5 1 系统基本设置 1.1 安装Ubuntu10.0 ...
- Seay工具分享
百度网盘:http://pan.baidu.com/share/home?uk=4045637737&view=share#category/type=0
- SDUT 2622 最短路径(Dijkstra)
点我看题目 题意 :中文不详述. 思路 :因为这个题加了一个要求就是路径数目得是x的倍数.所以在原来算法的一维dis数组增加到二维,用来存走的路径数%x.也可以用spfa做. #include < ...
- 来看看Meteor的功能
看了一上午,感觉这确实比所谓传统的APP开发,有很多不一样的地方. 记录下来: simple-todos.css /* CSS declarations go here */ /* CSS decla ...
- asp.net网站后台退出后,点后退按钮仍能进,如何安全退出
用户登录成功后,将用户名保存Session Session["usrename"]=username; 退出后Sessssion["username"]=str ...