字符串(后缀自动机):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 ...
随机推荐
- 适配器控件-Adapter
适配器对象派生自Android.widget.Adapter,它的作用包括:构造列表项控件,并将数据项绑定到列表项控件中. 常见的适配器有:数组适配器 ArrayAdapter,数据库适配器 Curs ...
- Linux网络基础
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/3840284.html ...
- PetaPoco 增删改查
1 查询单行 DBInstance.DB.SingleOrDefault<CompanyInfo11>(id); /// <summary> /// 根据id获取公司信息 // ...
- CSS Clip剪切元素实例
一.实例1: .fixed { position: fixed; width: 382px; height: 100px; background: red; border: 1px solid blu ...
- 使用EMMET中的小坑
使用EMMET写HTML的时候,是一个非常爽的事情.但是今天我使用时,发现一个小坑.以前倒也没有注意,不过需要非常的小心. form[action="/process" metho ...
- app图标和启动页设置
弄了一下午,终于把iOS中图标的设置和启动页的设置弄明白了.我想以后再也不会浑了. 进入正题: 一:apple 1).iPhone4s 3.5寸屏,也就是640*960,但在模拟器上正常用的是320* ...
- java 保留小数点后N位数(若干位),几种实现的方式总结
import java.math.BigDecimal;import java.text.DecimalFormat;import java.text.NumberFormat;/** * java ...
- Codeforces 475 D.CGCDSSQ
题目说了a的范围小于10^9次方,可实际却有超过的数据...真是醉了 算出以f[i]结尾的所有可能GCD值,并统计: f[i]可以由f[i-1]得出. /* 递推算出所有GCD值,map统计 */ # ...
- 那些年,我们一起被坑的H5音频
原文地址:http://weibo.com/p/23041874d6cedd0102vkbr 不要被这么文艺的标题吓到,这里不会跟你讲述中学时期泡妞史,也不会有其它什么现实不该有而小说噼里啪啦不能 ...
- PHPCMS(2)PHPCMS V9 环境搭建(转)
转自:http://www.cnblogs.com/Braveliu/p/5072920.html PHPCMS V9的学习总结分为以下几点: [1]PHPCMS 简介 PHP原始为Personal ...