字符串(后缀自动机):Codeforces Round #129 (Div. 1) E.Little Elephant and Strings
3 seconds
256 megabytes
standard input
standard output
The Little Elephant loves strings very much.
He has an array a from n strings, consisting of lowercase English letters. Let's number the elements of the array from 1 to n, then let's denote the element number i as ai. For each string ai (1 ≤ i ≤ n) the Little Elephant wants to find the number of pairs of integers l and r (1 ≤ l ≤ r ≤ |ai|) such that substring ai[l... r] is a substring to at least k strings from array a (including the i-th string).
Help the Little Elephant solve this problem.
If you are not familiar with the basic notation in string problems, you can find the corresponding definitions in the notes.
The first line contains two space-separated integers — n and k (1 ≤ n, k ≤ 105). Next n lines contain array a. The i-th line contains a non-empty string ai, consisting of lowercase English letter. The total length of all strings ai does not exceed 105.
On a single line print n space-separated integers — the i-th number is the answer for string ai.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
3 1
abc
a
ab
6 1 3
7 4
rubik
furik
abab
baba
aaabbbababa
abababababa
zero
1 0 9 9 21 30 0
Let's assume that you are given string a = a1a2... a|a|, then let's denote the string's length as |a| and the string's i-th character as ai.
A substring a[l... r] (1 ≤ l ≤ r ≤ |a|) of string a is string alal + 1... ar.
String a is a substring of string b, if there exists such pair of integers l and r (1 ≤ l ≤ r ≤ |b|), that b[l... r] = a.
这道题很好啊……
本来要用后缀数组,现在我用的是后缀自动机。用set维护经过的字串有哪些,建成这棵trie的SAM后,连边fa[i]->i,然后DFS,把set启发式合并一下,要的信息就是size,处理答案时先跳(必定可行),然后size<k就不停跳fa,直到size>=k为止,这时匹配的答案加入计数。
#include <iostream>
#include <cstring>
#include <cstring>
#include <cstdio>
#include <set>
using namespace std;
const int N=;
int fa[N],ch[N][],len[N];
string s[N];set<int>t[N];
int tr[N][],last[N];
int lst,cnt,n,k,tot;
void Insert(int c){
int p=lst,np=lst=++cnt;len[np]=len[p]+;
while(p&&ch[p][c]==)ch[p][c]=np,p=fa[p];
if(!p)fa[np]=;
else{
int q=ch[p][c],nq;
if(len[q]==len[p]+)
fa[np]=q;
else{
len[nq=++cnt]=len[p]+;
fa[nq]=fa[q];fa[q]=fa[np]=nq;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
while(ch[p][c]==q)ch[p][c]=nq,p=fa[p];
}
}
}
int cntE,fir[N],to[N],nxt[N];
void addedge(int a,int b){
nxt[++cntE]=fir[a];
to[fir[a]=cntE]=b;
}
void Initialization(){
memset(fir,,sizeof(fir));
lst=cnt=;last[cntE=tot=]=;
}
set<int>Merge(set<int>a,set<int>b){
if(a.size()<b.size())swap(a,b);
a.insert(b.begin(),b.end());
delete &b;return a;
}
void DFS(int x){
for(int i=fir[x],g;i;i=nxt[i])
DFS(g=to[i]),t[x]=Merge(t[x],t[g]);
} int main(){
Initialization();
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++){
cin>>s[i];
for(int j=,p=,c;j<s[i].size();j++){
c=s[i][j]-'a';lst=last[p];
if(tr[p][c])p=tr[p][c],t[last[p]].insert(i);
else Insert(c),t[last[p=tr[p][c]=++tot]=lst].insert(i);
}
}
for(int i=;i<=cnt;i++)
addedge(fa[i],i);DFS();
for(int i=;i<=n;i++){
long long ans=;
for(int j=,p=,c,l=;j<s[i].size();j++){
c=s[i][j]-'a';
p=ch[p][c];l+=;
while(p!=&&t[p].size()<k)
p=fa[p],l=len[p];
ans+=l;
}
printf("%I64d ",ans);
}
printf("\n");
return ;
}
字符串(后缀自动机):Codeforces Round #129 (Div. 1) E.Little Elephant and Strings的更多相关文章
- Codeforces Round #129 (Div. 1)E. Little Elephant and Strings
		
题意:有n个串,询问每个串有多少子串在n个串中出现了至少k次. 题解:sam,每个节点开一个set维护该节点的字符串有哪几个串,启发式合并set,然后在sam上走一遍该串,对于每个可行的串,所有的fa ...
 - 字符串处理/贪心 Codeforces Round #307 (Div. 2) B. ZgukistringZ
		
题目传送门 /* 题意:任意排列第一个字符串,使得有最多的不覆盖a/b字符串出现 字符串处理/贪心:暴力找到最大能不覆盖的a字符串,然后在b字符串中动态得出最优解 恶心死我了,我最初想输出最多的a,再 ...
 - Codeforces Round #129 (Div. 2)
		
A. Little Elephant and Rozdil 求\(n\)个数中最小值的个数及下标. B. Little Elephant and Sorting \[\sum_{i=1}^{n-1}{ ...
 - Codeforces Round #129 (Div. 2) C
		
Description The Little Elephant very much loves sums on intervals. This time he has a pair of intege ...
 - Codeforces Round #129 (Div. 2) B
		
Description The Little Elephant loves sortings. He has an array a consisting of n integers. Let's nu ...
 - Codeforces Round #129 (Div. 2) A
		
Description The Little Elephant loves Ukraine very much. Most of all he loves town Rozdol (ukr. &quo ...
 - Codeforces Round #136 (Div. 1)C. Little Elephant and Shifts multiset
		
C. Little Elephant and Shifts Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/pro ...
 - Codeforces Round #157 (Div. 1) B. Little Elephant and Elections 数位dp+搜索
		
题目链接: http://codeforces.com/problemset/problem/258/B B. Little Elephant and Elections time limit per ...
 - Codeforces Round #471 (Div. 2)B. Not simply beatiful strings
		
Let's call a string adorable if its letters can be realigned in such a way that they form two conseq ...
 
随机推荐
- asp.net 的状态管理
			
状态管理 (state management) 在Web应用程序中,一向是很重要的课题,良好的状态管理可以帮助开发人员发展出具有状态持续能力的应用程序(像是工作流程型应用程序或是电子商务应用程序),但 ...
 - ES6数组去重
			
今天五一,在出去玩之前赶紧写篇博客,时刻不要忘记学习^_^!! 提到数组去重,想必大家都不陌生,会的同学可能噼里啪啦写出好几个,下面来看看之前常见的去重代码: 'use strict'; var ar ...
 - Android开发手记(27) Java多线程的操作
			
Java中常用的有关线程的操作有,判断线程是否启动.线程强制执行.线程休眠.线程中断.线程让步.线程同步等.下面就一一举例. 首先,我们新建一个MyThread类实现Runnable接口.基于此接口进 ...
 - HTML5+移动APP(1)
			
前言: 介绍使用html5+(nativejs)和mui开发移动app(包括Android和iOs) HBuilder h5+开发app的环境,是一个对eclipse做了深度定的IDE. 官网: ht ...
 - (转)[开发笔记]-js判断用户的浏览设备是移动设备还是PC
			
<script type="text/javascript"> function browserRedirect() { var sUserAgent = naviga ...
 - MSSQLSERVER未分离LDF删除情况下的MDF附加
			
经过网上资料搜索,此方法可以解决. LDF日志不要轻易删除,恢复主数据要用到,如果删除,记得先分离,然后移动到另外的地方. 下面是针对未分离删除日志文件,MDF文件附加,提示找不到日志的问题的解决方法 ...
 - 某PHP代码加密
			
<?php /* 本程序已加密: 2014-11-15 10:10:11 */ xs_run('JGxosS9QplmqLA6qjYo/LiX5ecUe0DH7p42Ww/Mdkf5/ybZDs ...
 - 在treeview外加一个滚动条的实现
			
前台代码: <div style="overflow:auto;width:190px;height:280px;border:1px solid #336699;padding-le ...
 - Codeforces 475 D.CGCDSSQ
			
题目说了a的范围小于10^9次方,可实际却有超过的数据...真是醉了 算出以f[i]结尾的所有可能GCD值,并统计: f[i]可以由f[i-1]得出. /* 递推算出所有GCD值,map统计 */ # ...
 - 生成器模式(Builder)
			
1.本质:分离整体构建算法和部分构造 2.示意图: 3.功能: 构建复杂的产品,而且是细化的.分步骤的构建产品 分离构建算法和具体的构建实现 具体的构造实现可以方便的切换和扩展 4.优点: 1.松散耦 ...