E. Little Elephant and Strings
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

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.

Examples
Input
3 1
abc
a
ab
Output
6 1 3 
Input
7 4
rubik
furik
abab
baba
aaabbbababa
abababababa
zero
Output
1 0 9 9 21 30 0 
Note

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的更多相关文章

  1. Codeforces Round #129 (Div. 1)E. Little Elephant and Strings

    题意:有n个串,询问每个串有多少子串在n个串中出现了至少k次. 题解:sam,每个节点开一个set维护该节点的字符串有哪几个串,启发式合并set,然后在sam上走一遍该串,对于每个可行的串,所有的fa ...

  2. 字符串处理/贪心 Codeforces Round #307 (Div. 2) B. ZgukistringZ

    题目传送门 /* 题意:任意排列第一个字符串,使得有最多的不覆盖a/b字符串出现 字符串处理/贪心:暴力找到最大能不覆盖的a字符串,然后在b字符串中动态得出最优解 恶心死我了,我最初想输出最多的a,再 ...

  3. Codeforces Round #129 (Div. 2)

    A. Little Elephant and Rozdil 求\(n\)个数中最小值的个数及下标. B. Little Elephant and Sorting \[\sum_{i=1}^{n-1}{ ...

  4. Codeforces Round #129 (Div. 2) C

    Description The Little Elephant very much loves sums on intervals. This time he has a pair of intege ...

  5. Codeforces Round #129 (Div. 2) B

    Description The Little Elephant loves sortings. He has an array a consisting of n integers. Let's nu ...

  6. Codeforces Round #129 (Div. 2) A

    Description The Little Elephant loves Ukraine very much. Most of all he loves town Rozdol (ukr. &quo ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Raphaël.js学习笔记

    Rapheal.js 是一个矢量图绘图库.对于支持HTML5 SVG的浏览器使用SVG绘图,不支持SVG的IE(ie6,7,8)使用VML绘图.所以Raphael.js的兼容性非常好. Raphael ...

  2. 11月15日jquery学习笔记

    1.属性 jQuery对象是类数组,拥有length属性和介于0~length-1之间的数值属性,可以用toArray()方法将jQuery对象转化为真实数组. selector属性是创建jQuery ...

  3. c#中使用数据读取器读取查询结果

    今天有时间了. 在看<c#数据库入门经典> ,总结数据读取器查询结果. 针对单个结果集使用读取器,有3中方法: String connString =..; String sql =@&q ...

  4. 文字排版--下划线(text-decoration:underline)

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  5. SGU 118.Digital root

    时间限制:0.25s 空间限制:4M 题目大意 给出n个数,求n1+n1*n2+n1*n2*n3+n1...nn 的数根,数根是一个数各个位置数字和的树根,个位数的数根为它本身. 例如,f[987]= ...

  6. Swift中的dispatch_once 单例模式

    以下有三种方法实现单例模式,支持懒初始化和线程安全 全局变量 结构 dispatch_once 全局变量: 这里使用了全局变量而非类变量,是因为不支持类变量 private let _Singleto ...

  7. Thinkphp 事物问题

    $m=D('YourModel');//或者是M(); $m2=D('YouModel2'); $m->startTrans();//在第一个模型里启用就可以了,或者第二个也行 $result= ...

  8. fish code

    <embed width="272" height="180" type="application/x-shockwave-flash" ...

  9. 在Lufylegend中如何设置bitmap或者sprite的缩放和旋转中心

    最近两天有个lufylegend游戏引擎群的群友需要做一个项目,其中要解决的需求是:获取照相机拍摄的图片,根据图片的EXIF信息让图片显示为“正常”情况,并且需要给图片添加一些事件侦听.何为正常呢?就 ...

  10. YII 登陆时 session持久化

    在YII框架中,session持久化方法只需要调用login()方法就行了 class loginAction extends CAction{ function run(){ $model=new ...