Codeforces Round #129 (Div. 1)E. Little Elephant and Strings
题意:有n个串,询问每个串有多少子串在n个串中出现了至少k次.
题解:sam,每个节点开一个set维护该节点的字符串有哪几个串,启发式合并set,然后在sam上走一遍该串,对于每个可行的串,所有的fail都是可行的直接加上len,不可行就往fail上跳.
for(int i=0;s[i];i++)有问题!!!!
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
//#define C 0.5772156649
//#define ls l,m,rt<<1
//#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
//#define base 1000000000000000000
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
using namespace std;
const ull ba=233;
const db eps=1e-5;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=100000+10,maxn=1000000+10,inf=0x3f3f3f3f;
int n,k;
char s[N];
vector<char>v[N];
struct SAM{
int last,cnt;
int ch[N<<1][26],fa[N<<1],l[N<<1];
int sz[N<<1],id[N<<1];
set<int>st[N<<1];
vi son[N<<1];
SAM(){cnt=1;}
void ins(int c){
if(ch[last][c])
{
int p=last,q=ch[last][c];
if(l[q]==l[p]+1)last=q;
else
{
int nq=++cnt;l[nq]=l[p]+1;
memcpy(ch[nq],ch[q],sizeof ch[q]);
fa[nq]=fa[q];fa[q]=last=nq;
for(;ch[p][c]==q;p=fa[p])ch[p][c]=nq;
}
return ;
}
int p=last,np=++cnt;last=np;l[np]=l[p]+1;
for(;p&&!ch[p][c];p=fa[p])ch[p][c]=np;
if(!p)fa[np]=1;
else
{
int q=ch[p][c];
if(l[p]+1==l[q])fa[np]=q;
else
{
int nq=++cnt;l[nq]=l[p]+1;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
fa[nq]=fa[q];fa[q]=fa[np]=nq;
for(;ch[p][c]==q;p=fa[p])ch[p][c]=nq;
}
}
}
void build(int id)
{
last=1;int len=strlen(s);
for(int i=0;i<len;i++)ins(s[i]-'a'),st[last].insert(id);
}
void dfs(int u)
{
for(int x:son[u])
{
dfs(x);
if(st[id[u]].size()<st[id[x]].size())
{
for(int p:st[id[u]])st[id[x]].insert(p);
st[id[u]].clear();id[u]=id[x];
}
else
{
for(int p:st[id[x]])st[id[u]].insert(p);
st[id[x]].clear();
}
}
sz[u]=st[id[u]].size();
}
void cal()
{
for(int i=2;i<=cnt;i++)son[fa[i]].pb(i),id[i]=i;
id[1]=1;
dfs(1);
for(int i=1;i<=n;i++)
{
int now=1;ll ans=0;
for(int j=0;j<v[i].size();j++)
{
now=ch[now][v[i][j]-'a'];
while(now!=1&&sz[now]<k)now=fa[now];
ans+=l[now];
}
printf("%lld ",ans);
}
puts("");
}
}sam;
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%s",s);
int n=strlen(s);
for(int j=0;j<n;j++)v[i].pb(s[j]);
sam.build(i);
}
sam.cal();
return 0;
}
/********************
********************/
Codeforces Round #129 (Div. 1)E. Little Elephant and Strings的更多相关文章
- 字符串(后缀自动机):Codeforces Round #129 (Div. 1) E.Little Elephant and Strings
E. Little Elephant and Strings time limit per test 3 seconds memory limit per test 256 megabytes inp ...
- 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 #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) B. Little Elephant and Array
B. Little Elephant and Array time limit per test 4 seconds memory limit per test 256 megabytes input ...
- Codeforces Round #157 (Div. 2) D. Little Elephant and Elections(数位DP+枚举)
数位DP部分,不是很难.DP[i][j]前i位j个幸运数的个数.枚举写的有点搓... #include <cstdio> #include <cstring> using na ...
随机推荐
- linux目录详细列表
详细列表 目录 说明 备注 bin 存放普通用户可执行的指令 即使在单用户模式下也能够执行处理 boot 开机引导目录 包括Linux内核文件与开机所需要的文件 dev 设备目录 所有的硬件设备及周边 ...
- Python记录wsgi
类实现wsgi app from wsgiref.util import setup_testing_defaults from wsgiref.simple_server import make_s ...
- 利用BLEU进行机器翻译检测(Python-NLTK-BLEU评分方法)
双语评估替换分数(简称BLEU)是一种对生成语句进行评估的指标.完美匹配的得分为1.0,而完全不匹配则得分为0.0.这种评分标准是为了评估自动机器翻译系统的预测结果而开发的,具备了以下一些优点: 计算 ...
- 2018-2019-2 网络对抗技术 20165335 Exp3 免杀原理与实践
一.免杀原理与基础知识: (1)杀软是如何检测出恶意代码的? 检测特征码:特征码就是一般程序都不会有的代码,而后门有的那种特别的数据,而一个程序,应用有这种代码,数据的话,就直接判定为恶意代码. 主流 ...
- redis参数说明
感谢网友分享. 1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程 daemonize no 2. 当Redis以守护进程方式运行时,Redis默认会把pid写 ...
- python接口自动化-接口重定向和编码格式
1.如果测试的接口重定向,那么只需添加allow_redirects=False禁止重定向就可: 2.如果请求的结果,中文出现没有解码 \xe9\x83\xad\xe7\xbf\xa0\xe6\x99 ...
- 用keytool制作证书并在tomcat配置https服务(一)
https分为单项认证和双向认证. 一般https页面上的访问都是单项认证,服务端发送数字证书给客户端,客户单方面验证.而服务端不做验证. 而双向认证,需要双方都有证书,然后发送给对方进行验证.一般用 ...
- 16: mint-ui移动端
1.1 mint-ui安装与介绍 官网:http://mint-ui.github.io/docs/#/zh-cn2/loadmore 1.安装与引用 // 安装Vue 2.0 npm instal ...
- eclipse如何忽略、过滤不需要提交到svn的文件和目录
1.进入navigator视图 2.选中不需要提交到svn的目录或者文件,右键team->添加至svn:ignore 3.如图:
- pyhton抛出自定义的异常
用raise语句来引发一个异常.异常/错误对象必须有一个名字,且它们应是Error或Exception类的子类 下面是一个引发异常的例子: class ShortInputException(Exce ...