Yong Zheng's Death

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 224    Accepted Submission(s): 37

Problem Description
Some Chinese emperors ended up with a mysterious death. Many
historians like to do researches about this. For example, the 5th Qing
dynasty emperor Yong Zheng's death is often argued among historians.
Someone say that he was killed by the top assassin Lu Siniang whose
family were almost wiped out by Yong Zheng. Someone say that Yong Zheng
was poisoned to death because he liked to eat all kinds of Chinese
medicines which were said to have the functions of prolonging human
lives. Recently, a new document was discovered in Gu Gong(the Forbidden
City). It is a secret document written by Yong Zheng's most trusted
eunuch. It reads on the cover: "This document tells how Yong Zheng died.
But I can't write this in plain text. Whether people will understand
this is depend on Gods." Historians finally found out that there are
totally n strings in the document making a set S = { s1,s2,... sn}. You
can make some death ciphers from the set. A string is called a DEATH
CIPHER if and only if it can be divide into two substrings u and v, and u
and v are both a prefix of a string from S (Note that u and v can't be
empty strings, and u and v can be prefixes of a same string or two
different strings).

When all DEATH CIPHERs are put together, a readable article revealing Yong Zheng's death will appear.

Please help historians to figure out the number of different death ciphers.

 
Input
The input consists of no more than 20 test cases.

For each case, the first line contains an integer n(1 <= n <= 10000), indicating the number of strings in S.

The following n lines contain n strings, indicating the strings in S.
Every string only consists of lower case letters and its length is
between 1 and 30(inclusive).

The input ends by n = 0.

 
Output
For each test case, print a number denoting the number of death ciphers.
 
Sample Input
2
ab
ac
0
 
Sample Output
9

Hint

For the sample , the death ciphers are {aa, aba, aca, aab, aac, abac, acab, abab, acac}

  神题啊!在此我会介绍两种方法,都是构造法,构造是很跳脱的,这也是此题难点,写题解时有些小激动。

  考虑答案的构成,会是两个前缀,我们叫它们A,B;网上很多WA的代码,就是建trie后输出cnt²,当然是错的,考虑当前枚举的A+B答案串,可能有多断点可以使得断点左边是A,右边是B,比如数据:3 abc bc c,这里abc答案串会被枚举两次,所以错误的原因是算重复了。

  先建一个AC自动机。

两种解决思路:

  第一种是补集转换,用总的-不合法的,总的就是cnt²,不合法的我们来分析,下图是某个答案串的情形。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgsAAABtCAIAAACgD7X3AAADnUlEQVR4nO3bPW5aQRiFYS+EktVQeS0UrMWSl2J5KWYHFBYFQrKUFMgxRsdK8Ixz73w8j2jSRFdHzH35890vAEjupr4AAGZKIQDIFAKATCEAyBQCgEwhAMgUAoBMIQDIFAKATCEAyBQCgEwhAMgUAoBMIQDIFAKATCEAyBQCgEwhAMgUAoBMIQDIFAKATCEAyBQCgEwhAMgUAoBMIQDIFAKATCEAyBQCuMrzfrHcvT/2T1NfDj9JIWbq7eF+t1i+PmynvpA6juuP+5ptv+f0tDyvwnF9f3iZ7oL4YQoxT9vDarlfb3arx7epL6WM43r5sefL46tIXOtps1vowW1RiFl62uwWm+PL46t38f18KsTpn+vnKS9oMNvDSlNvjkLM0fvNa3tYuYt187kQtr2S1ys3SSHm5+wovj3ce1/fy6dCnN6lTXtBY3l5fPVUvD0KMTtvD/cfNy8fl/fjm+omCnGTFGJunveL808/tofV0qvdLsKnTH4IcIXnvazeHoWYmafN+etcvzrv6OKbar/MudZx7cXKzVGIWYkvbL3a7UMhmp3e0Z6Ptj2sbFiZQszJVz8XcS/rwadMXZz+aO794WlZnELMyNfv4i++nOA7Lr6ptif8lUIAkCkEAJlCAJApBACZQgCQKQQAmUIAkCkEAJlCAJApBACZQgCQKQQAmUIAkCkEAJlCAJApBACZQgCQKQQAmUIAkCkEAJlCAJB9pxB3AIxJIarZLZanx9QXAgxPIUr5kweRANopRCkK0YsNW1ivDIUoRSG6MGML61Xyvwvxjf+Bf3dxOKe+nFGZsYX1hqYQlTmcXZixhfWGphCVOZxdmLGF9YamEJU5nF2YsYX1hqYQlTmcXZixhfWGphCVOZxdmLGF9YamEJU5nF2YsYX1hqYQlTmcXZixhfWGphCVOZxdmLGF9YamEJU5nF2YsYX1hqYQlTmcXZixhfWGphCVOZxdmLGF9YamEJU5nF2YsYX1hqYQlTmcXZixhfWGphCVOZxdmLGF9YamEJU5nF2YsYX1hqYQlTmcXZixhfWGNnEh+FEXh3PqyxmVGVtYrxKFKMXh7MKMLaxXiUKU4nB2YcYW1qtEIUpxOLswYwvrVaIQ1TiZ7dzjWlivkh8vBIzIT3FaWO82KQQAmUIAkCkEAJlCAJApBACZQgCQKQQAmUIAkCkEAJlCAJApBACZQgCQKQQAmUIAkCkEAJlCAJApBACZQgCQKQQA2W872zlpaVUPdQAAAABJRU5ErkJggg==" alt="" />

  这里已经假设A,B,C三个断点都是可以的,这表示[1~A],[1~B],[1~C],[A+1~len],[B+1~len],[C+1~len]都是前缀,总的方案数是3,不合法的方案数是2,现在来构造方法(我有三种方法):

  1.求中间没有红线的两头都是红线的段数(A~B,B~C)

  2.求左端点是最左边的红线的段数

  3.求右端点是最右边的红线的段数

  我们采用第一种方法,枚举前缀B,假设与某个A构成的A+B答案串中有当前B位置后面的断点,fail[B]处一定是最近的那个。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAApMAAADDCAIAAABRZQ10AAAIDElEQVR4nO3cT07qUBiHYRfCkNUwYi0MWIsJSyEsBXbAgDAgJCS9A/9cbFFrQU9/7fPkm4Do7U30vJ7S+lQBADmeSh8AAPADyg0ASZQbAJIoNwAkUW4ASKLcAJBEuQEgiXIDQBLlBoAkyg0ASZQbAJIoNwAkUW4ASKLcAJBEuQEgiXIDQBLlBoAkyg0ASZQbAJIoNwAkUW4ASKLcAJBEuQEgiXIDQBLlBoAkyg0ASZQbAJIoNwAkUW4ASKLcAJBEuQEgiXIDQBLlBoAkyg0ASZQbAJIo95+6PM/3i03powAgmHL/le3qMJnuJ1PlBuAeyv0XLs/z/Wx1qarzQrkBuItyP8Z5Md3PVpf3jfUne2vlBuBOyv0Y58V0P5m+bKyr6u3ceCPSyg3AnZT7Mc6L6X6yPF89c3me156plBuAuyn3Y7yeLb9+ar3cT+anbeNlyg3AHZT7MZSbH9oc3y+JmEwPz7vSxwPEUO7HaJ4tv9Fy5ebNeXFV6/VSvIH2lPsxXq9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mUGwCSKDcAJFFuAEii3ACQRLkBIIlyA0AS5QaAJMoNAEmUGwCSKDcAJFFuAEii3ACQRLkBIMk/Mz/q0j5FFvAAAAAASUVORK5CYII=" alt="" />

  p1,p2为合法分割,fail[x]=p2,所以当前枚举的前缀B,我们可以接的A前缀只需要满足后缀是[p1~p2]即可。(这句话很重要)

代码很好理解:

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int N=;
typedef long long LL;
int fa[N],len[N],deg[N];
int cnt,ch[N][],fail[N];
LL sum[N];queue<int>q;
char s[N];
struct ACM{
void Init(){
memset(fa,,sizeof(fa));
memset(sum,,sizeof(sum));
memset(deg,,sizeof(deg));
memset(fail,,sizeof(fail));
memset(ch,,sizeof(ch));cnt=;
}
void Insert(char *s){
int l=strlen(s);
for(int i=,p=;i<l;i++){
int c=s[i]-'a';
if(ch[p][c])p=ch[p][c];
else{
fa[++cnt]=p;
len[cnt]=len[p]+;
p=ch[p][c]=cnt;
}
}
}
void Build(){
for(int i=;i<;i++)
if(ch[][i])q.push(ch[][i]);
while(!q.empty()){
int x=q.front();q.pop();
for(int i=;i<;i++)
if(ch[x][i]){
fail[ch[x][i]]=ch[fail[x]][i];
q.push(ch[x][i]);
}
else ch[x][i]=ch[fail[x]][i];
}
for(int i=;i<=cnt;i++)
sum[i]=;
for(int i=;i<=cnt;i++)
if(fail[i])deg[fail[i]]++;
for(int i=;i<=cnt;i++)
if(!deg[i])q.push(i);
while(!q.empty()){
int x=q.front();q.pop();
sum[fail[x]]+=sum[x];
if(!--deg[fail[x]])
q.push(fail[x]);
}
}
LL Solve(){
LL ret=;
for(int i=;i<=cnt;i++){
int tmp=len[fail[i]],p=i;
while(tmp--)p=fa[p];
ret+=(fail[i]!=)*(sum[p]-);
}
return 1ll*cnt*cnt-ret;
}
}ac;
int n;
int main(){
while(true){
scanf("%d",&n);
if(!n)break;ac.Init();
for(int i=;i<=n;i++){
scanf("%s",s);
ac.Insert(s);
}
ac.Build();
printf("%lld\n",ac.Solve());
}
return ;
}

第二种方法是直接正着搞,DP出合法的即可(膜的poursoul大大的代码)

  这也是构造,我先跑A串,使其尽量长,直到没有某个转移,不得不跳fail时开始DP,先上图。

  有时候构造没有理由,不好理解不好发现,不过可以证明是对的。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAk0AAAEECAIAAAC6J5c2AAAQzElEQVR4nO3dT27iyALA4TkIS07DirOw4CxEOcJs3j7DFXrfi6BePM2iJfqJQSMaCclvAQb/hSQUdrn4PtUioYkDZOTfVNmGPzIASNcffT8AAHggnQMgZToHQMp0DoCU6RwAKdM5AFKmcwCkTOcASJnOAZAynQMgZToHQMp0DoCU6RwAKdM5AFKmcwCkTOcASJnOAZAynQMgZToHQMp0rie///3z+8+X7z//XB/a7rL6++fLj39/1b5++NYAEqJzvfj919UmZVmWZYdvP1rK9M/m5fvPl+8/X/7+/YCtNfi1Xr8UOlr5FiBmOteH3//++X39rTUrDaplKs7Gwm6t1eHbj2LYKt8CRErn+vDP5iVgmcJurZXOAYOkc11b/f3ztE74/Wfep8O3H+dbNqviPdtWGou3h9vacUHyr3/OD/b3X5f1TJ0DBknn+lCegf1ab/KvW4+ifXw+d+fWCv90+PajeNBO54BB0rk+XFlp/GdznoR9rXP3bi0/q+XXel2cDuocMFA614damcrLj/d27q6tZeerFCrt1DlgkHSuD6Uy/f6reCDt3vnc3VvLdA5Iis71oVim6tG19V2du39recB+rdeNt7d8CxApnetDtUz5KY6nidSdnbtra85DARKjc31omHX9fPn+8+XHv7/uPg/lnq2t/i4vVx5L6boCYMh0boA+emV32K3pHDBIOjdAOgfwYTo3QB945+WwW/M+zsBw6RwAKdM5AFKmcwCkTOcASJnOAZAynQMgZToHQMp0DoCU6RwAKdM5AFKmcwCk7OGdO713ovGl8ei/DkDydC7qkWXZejR+9Hj0fwMAPdK5qEemcwD30bmoR6ZzAPfRuahHpnMA9+m6c4/+dUOncwBh6VxcbnYuyG/ROeB56FxcdA4gLJ2Li84BhKVz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nV/+v66aftYuBY69wg6BxBGdX/6Nm/9+NMWOvcIOgcQRnF/Wv487mPtCp/fXfg0bcfnHk3nAMK4Pp87LOb5163H5HTuEXQOIIxPrFu+zc9TOp17NJ0DCONm58qLmTrXEZ0DCONq55bb4mE587kO6RxAGNc6Vz1WN9W5zugcQBi3OjfevmVZdn6TFJ3riM4BhHHrfMvp6cjc5PVg3bJDOgcQhvdxjpPOAYShc3HSOYAwdC5OOgcQhs7FSecAwtC5OOkcQBg6FyedAwhD5+KkcwBh6FycdA4gDJ2Lk84BhKFzcdI5gDB0Lk46BxCGzsVJ5wDC0Lk46RxAGDoXJ50DCEPn4qRzAGHoXJx0DiAMnYuTzgGEcfq4cCO+cc+fVecASJnOAZAynQMgZToHQMp0DoCU6RwAKdM5AFKmcwCkTOcASJnOAZAynQMgZToHQMp0DoCU/R/V4tbr6tIAxwAAAABJRU5ErkJggg==" alt="" />

  现在于x计数,后面接的B串都是上图中第一二行的形式(上面应有省略号),我用我的感受引入一个乱yy的名词,"潜力",这里枚举出的最左断点是fail[x],fail[fail[x]],fail[fail[fail[x]]]……最开始时1~fail[x]那么长的一段就是ch[x][i](注意这里跳了fail)状态的"潜力",枚举B串,如果不能转移就跳fail,最后"潜力"没了,没了就不能跳fail了,即不能匹配了。

  可以发现枚举的过程中"潜力"一直是尽可能的大,就是指断点在最左边。

  dp[i][x]表示原x(和这里的x无关)位置后面匹配i个长度,在状态x的方案数,发现是可以转移的。

  哎,讲不清讲不清,建议看代码,然后仔细地感受。

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int N=,M=;
typedef long long LL;
int ch[N][],fail[N],fa[N],sln[N];
int n,cnt;LL ans,dp[M][N];
char s[N];queue<int>q; struct AC{
void Init(){
memset(fa,,sizeof(fa));
memset(ch,,sizeof(ch));
memset(dp,,sizeof(dp));
memset(sln,,sizeof(sln));
memset(fail,,sizeof(fail));
ans=cnt=;
}
void Insert(char *s){
int len=strlen(s);
for(int i=,p=;i<len;i++){
int c=s[i]-'a';
if(ch[p][c])p=ch[p][c];
else{
sln[++cnt]=sln[p]+;
fa[cnt]=p;p=ch[p][c]=cnt;
}
}
}
void Build(){
for(int i=;i<;i++)
if(ch[][i])
q.push(ch[][i]); while(!q.empty()){
int x=q.front();q.pop();
if(fail[x]!=)ans=ans+;
for(int i=;i<;i++)
if(ch[x][i]){
fail[ch[x][i]]=ch[fail[x]][i];
q.push(ch[x][i]);
}
else{
ch[x][i]=ch[fail[x]][i];
if(ch[x][i]){
dp[][ch[x][i]]+=;
ans+=;
}
}
}
}
void Solve(){
bool flag=;
for(int i=;flag;i++){
flag=;
for(int j=;j<=cnt;j++)if(dp[i-][j]){
for(int k=,t;k<;k++){
if(sln[t=ch[j][k]]<i)continue;
dp[i][t]+=dp[i-][j];flag=;
}
}
for(int j=;j<=cnt;j++)ans+=dp[i][j];
}
printf("%lld\n",ans);
}
}ac;
int main(){
while(~scanf("%d",&n)&&n){
ac.Init();
for(int i=;i<=n;i++){
scanf("%s",s);
ac.Insert(s);
}
ac.Build();ac.Solve();
}
return ;
}

字符串(AC自动机):HDU 5129 Yong Zheng's Death的更多相关文章

  1. HDU 5129 Yong Zheng's Death

    题目链接:HDU-5129 题目大意为给一堆字符串,问由任意两个字符串的前缀子串(注意断句)能组成多少种不同的字符串. 思路是先用总方案数减去重复的方案数. 考虑对于一个字符串S,如图,假设S1,S2 ...

  2. 模板—字符串—AC自动机(多模式串,单文本串)

    模板—字符串—AC自动机(多模式串,单文本串) Code: #include <queue> #include <cstdio> #include <cstring> ...

  3. 数据结构--AC自动机--hdu 2896

    病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  4. 从0开始 数据结构 AC自动机 hdu 2222

    参考博客 失配指针原理 使当前字符失配时跳转到另一段从root开始每一个字符都与当前已匹配字符段某一个后缀完全相同且长度最大的位置继续匹配,如同KMP算法一样,AC自动机在匹配时如果当前字符串匹配失败 ...

  5. [coci2012]覆盖字符串 AC自动机

    给出一个长度为N的小写字母串,现在Mirko有M个若干长度为Li字符串.现在Mirko要用这M个字符串去覆盖给出的那个字符串的.覆盖时,必须保证:1.Mirko的字符串不能拆开,旋转:2.Mirko的 ...

  6. 字符串——AC自动机

    目录 一.前言 二.思路 三.代码 四.参考资料 一.前言 以前一直没学AC自动机,主要是被名字吓到了,自动AC,这么强的名字肯定很难,学了后才发现,其实不难. AC自动机并不是Acept autom ...

  7. AC自动机 HDU 3065

    大概就是裸的AC自动机了 #include<stdio.h> #include<algorithm> #include<string.h> #include< ...

  8. AC自动机 HDU 2896

    n个字串 m个母串 字串在母串中出现几次 #include<stdio.h> #include<algorithm> #include<string.h> #inc ...

  9. 【bzoj1030】: [JSOI2007]文本生成器 字符串-AC自动机-DP

    [bzoj1030]: [JSOI2007]文本生成器 首先把匹配任意一个的个数的问题转化为总个数-没有一个匹配的个数 先构造AC自动机,然后枚举每一位的字母以及在自动机上的位置 f[i][j]为第i ...

随机推荐

  1. Mac 下显示隐藏文件

    将下面的命令粘贴进终端,按提示操作即可(可能需要输入电脑密码) 显示:defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏:d ...

  2. java新手笔记17 参数

    package com.yfs.javase; public class ParamDemo { public static void main(String[] args) { int a = 3, ...

  3. 访问Access数据库(有多个数据库时 体现多态)

    如果想编写单机版MIS.小型网站等对数据库性能要求不高的系统,又不想安装SQLServer,可以使用Access(MDAC),只要一个mdb文件就可以了.使用Access创建mdb文件,建表.OleD ...

  4. git push后修改错误的commit message

    Easiest solution (but please read this whole answer before doing this): git rebase -i <hash-of-co ...

  5. ECMAScript一元操作符

    在ECMAScript中提供了一元操作符进行简单的运算,一元操作符是ECMAScript中最简单的操作符,它只能对一个值进行操作. 一元操作符有两种类型,一种是递增和递减操作符,一种是一元加和一元减操 ...

  6. user-agent查询

    浏览器地址栏输入:javascript:window.navigator.userAgent通过网站查询:http://www.enhanceie.com/ua.aspxhttp://www.user ...

  7. python 数据运算

    算数运算:  

  8. 工作流(worfflow)

    -- 工作流(Workflow)就是“业务过程的部分或整体在计算机应用环境下的自动化”,它主要解决的是“使在多个参与者之间按照某种预定义的规则传递文档.信息或任务的过程自动进行,从而实现某个预期的业务 ...

  9. cocos2dx 3.3创建新项目 和 VS2012解决方案加载失败问题

     首先创建新项目,步骤如下: 1.进入cocos2d-x-3.3\tools\cocos2d-console\bin目录,按住shift+鼠标右键 2.输入 cocos new 项目名 –p 包名 – ...

  10. iOS开发 - NSBundle, NSDevice, NSLocale

    iOS的APP的应用开发的过程中,有时为了bug跟踪或者获取用反馈的需要自动收集用户设备.系统信息.应用信息等等,这些信息方便开发者诊断问题,当然这些信息是用户的非隐私信息,是通过开发api可以获取到 ...