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. Axiom3D学习日记 0.Axiom基础知识

    Axiom 3D Engine An open-source, cross-platform, managed 3D rendering engine for DirectX, XNA and Ope ...

  2. 安装Visual Studio 2010时提示"The location specified for the help content store is invalid or you do not have access to it".

    运行注册表: (运行->输入"regedit").在 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Help\v1.0中,删除"Loc ...

  3. 【转】iOS- 详解文本属性Attributes

    原文: http://www.cnblogs.com/qingche/p/3574995.html?utm_source=tuicool 1.NSKernAttributeName: @10 调整字句 ...

  4. 2016.7.16equals的使用(一)

    class V{ private int a; V(int a){ rhis a=a; } public  boolean equals(int a,int b){ if(this.a equals( ...

  5. c++primerplus(第六版)编程题——第5章(循环和关系表达式)

    声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. (具体方式参见第3章模板) 1. ...

  6. kafka监控

    做kafka相关项目,需要用到监控的东东,找到了两个方法 第一个: http://quantifind.com/KafkaOffsetMonitor/ 非常容易上手,效果也直观,可以显示各个topic ...

  7. 下拉框——把一个select框中选中内容移到另一个select框中遇到的问题

    在使用jQuery实现把一个select框中选中内容移到另一个select框中功能时遇到了一个问题,就是点击按钮时内容可以到另一个select框中,但是到了另一个select框中的内容却很快闪退回原来 ...

  8. 控制寄存器 CR*

    控制寄存器(CR0-CR3)用于控制和确定处理器的操作模式以及当前执行任务的特性,如图4-3所示.CR0中含有控制处理器操作模式和状态的系统控制标志:CR1保留不用:CR2含有导致页错误的线性地址:C ...

  9. bzoj2597: [Wc2007]剪刀石头布

    Description 在一些一对一游戏的比赛(如下棋.乒乓球和羽毛球的单打)中,我们经常会遇到A胜过B,B胜过C而C又胜过A的有趣情况,不妨形象的称之为剪刀石头布情况.有的时候,无聊的人们会津津乐道 ...

  10. uitextfield动态限制输入的字数-b

    1.定义一个事件: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 ...