HDU 5343 MZL's Circle Zhou 后缀自动机+DP
MZL's Circle Zhou
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
You are given two strings a,b which consist of only lowercase English letters. You can subtract a substring x (maybe empty) from string a and a substring y (also maybe empty) from string b, and then connect them as x+y with x at the front and y at the back. In this way, a series of new strings can be obtained.
The question is how many different new strings can be obtained in this way.
Two strings are different, if and only if they have different lengths or there exists an integer i such that the two strings have different characters at position i.
For each test case, there are two lines, the first line is string a, and the second line is string b. 1<=|a|,|b|<=90000.
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
inline long long read(){long long x=,f=;char ch=getchar();while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}return x*f;}
using namespace std; const long long INF = 1e14;
const int N = 5e5+;
const long long mod = ; struct SAM{
int isPlus[N * ],endpos[N * ];int d[N * ];
int cnt[N * ];int pos[N];
int tot,slink[*N],trans[*N][],minlen[*N],maxlen[*N],pre;
int newstate(int _maxlen,int _minlen,int* _trans,int _slink){
maxlen[++tot]=_maxlen;minlen[tot]=_minlen;
slink[tot]=_slink;
if(_trans)for(int i=;i<;i++)trans[tot][i]=_trans[i];
return tot;
}
int add_char(char ch,int u){
int c=ch-'a',v=u;
int z=newstate(maxlen[u]+,-,NULL,);
isPlus[z] = ;
while(v&&!trans[v][c]){trans[v][c]=z;d[z]+=;v=slink[v];}
if(!v){ minlen[z]=;slink[z]=;return z;}
int x=trans[v][c];
if(maxlen[v]+==maxlen[x]){slink[z]=x;minlen[z]=maxlen[x]+;return z;}
int y=newstate(maxlen[v]+,-,trans[x],slink[x]);
slink[z]=slink[x]=y;minlen[x]=minlen[z]=maxlen[y]+;
while(v&&trans[v][c]==x){trans[v][c]=y;d[x]--,d[y]++;v=slink[v];}
minlen[y]=maxlen[slink[y]]+;
return z;
}
void init_sam() {
for(int i = ; i <= tot; ++i)
for(int j = ; j < ; ++j) trans[i][j] = ;
pre = tot = ;
}
}A,B; unsigned long long f[N],dp[N];
char a[N],b[N];
int main() {
int T,cas = ;
scanf("%d",&T);
while(T--) {
scanf("%s%s",a,b);
int n = strlen(a);
int m = strlen(b);
A.init_sam();
B.init_sam();
for(int i = ; i < n; ++i) A.pre = A.add_char(a[i],A.pre);
for(int i = ; i < m; ++i) B.pre = B.add_char(b[i],B.pre); for(int i = ; i <= m; ++i) B.cnt[i] = ;
for(int i = ; i <= B.tot; ++i) B.cnt[B.maxlen[i]] += ;
for(int i = ; i <= m; ++i) B.cnt[i] += B.cnt[i-];
for(int i = B.tot; i >= ; --i) B.pos[B.cnt[B.maxlen[i]]--] = i; for(int i = ; i <= n; ++i) A.cnt[i] = ;
for(int i = ; i <= A.tot; ++i) A.cnt[A.maxlen[i]] += ;
for(int i = ; i <= n; ++i) A.cnt[i] += A.cnt[i-];
for(int i = A.tot; i >= ; --i) A.pos[A.cnt[A.maxlen[i]]--] = i;
memset(f,,sizeof(f));
memset(dp,,sizeof(dp));
for(int i = B.tot; i >= ; --i) {
int v = B.pos[i];
f[v] = ;
for(int j = ; j < ; ++j) {
if(B.trans[v][j])f[v] += f[B.trans[v][j]];
}
}
for(int i = ; i < ; ++i)
dp[i] = f[B.trans[][i]];
unsigned long long ans = ;
for(int i = A.tot; i >= ; --i) {
int v = A.pos[i];
for(int j = ; j < ; ++j) {
if(A.trans[v][j] == ) {
ans += dp[j]*(A.maxlen[v] - A.minlen[v] + );
}
}
}
for(int i = A.tot; i >= ; --i) ans += (A.maxlen[i] - A.minlen[i] + );
cout<<ans+<<endl;
}
return ;
}
HDU 5343 MZL's Circle Zhou 后缀自动机+DP的更多相关文章
- HDU 5343 MZL's Circle Zhou
MZL's Circle Zhou Time Limit: 1000ms Memory Limit: 131072KB This problem will be judged on HDU. Orig ...
- hdu 5343 MZL's Circle Zhou SAM
MZL's Circle Zhou 题意:给定两个长度不超过a,b(1 <= |a|,|b| <= 90000),x为a的连续子串,b为y的连续子串(x和y均可以是空串):问x+y形成的不 ...
- HDU5343 MZL's Circle Zhou(SAM+记忆化搜索)
Problem Description MZL's Circle Zhou is good at solving some counting problems. One day, he comes u ...
- 【bzoj3998】[TJOI2015]弦论 后缀自动机+dp
题目描述 对于一个给定长度为N的字符串,求它的第K小子串是什么. 输入 第一行是一个仅由小写英文字母构成的字符串S 第二行为两个整数T和K,T为0则表示不同位置的相同子串算作一个.T=1则表示不同位置 ...
- HDU 1403 Longest Common Substring(后缀自动机——附讲解 or 后缀数组)
Description Given two strings, you have to tell the length of the Longest Common Substring of them. ...
- HDU 6208 The Dominator of Strings 后缀自动机
The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java ...
- HDU - 6583 Typewriter (后缀自动机+dp)
题目链接 题意:你要打印一段字符串,往尾部添加一个字符需要花费p元,复制一段字符到尾部需要花费q元,求打印完全部字符的最小花费. 一开始想的贪心,后来发现忘了考虑p<q的情况了,还纳闷怎么不对. ...
- bzoj 2806: [Ctsc2012]Cheat 后缀自动机DP
2806: [Ctsc2012]Cheat Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 583 Solved: 330[Submit][Statu ...
- BZOJ4032[HEOI2015]最短不公共子串——序列自动机+后缀自动机+DP+贪心
题目描述 在虐各种最长公共子串.子序列的题虐的不耐烦了之后,你决定反其道而行之. 一个串的“子串”指的是它的连续的一段,例如bcd是abcdef的子串,但bde不是. 一个串的“子序列”指的是它的可以 ...
随机推荐
- DB2 数据库中字段特定字符替换为空
Update RM_CarInfo set ImportTitle = Replace(ImportTitle,'ZD','') WHERE ImportTitle LIKE'%ZD%';
- FZU 1057 a^b 【数论/九余定理】
Accept: 1164 Submit: 3722Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description 对于任 ...
- 洛谷——P2626 斐波那契数列(升级版)
P2626 斐波那契数列(升级版) 题目背景 大家都知道,斐波那契数列是满足如下性质的一个数列: • f(1) = 1 • f(2) = 1 • f(n) = f(n-1) + f(n-2) (n ≥ ...
- Codeforces Round #324 (Div. 2) Olesya and Rodion 构造
原题链接:http://codeforces.com/contest/584/problem/A 题意: 给你n和t,让你构造一个长度为n的数,并且被t整除 题解: 方法很多,可以乱构造.....不过 ...
- Maven出现错误No plugin found for prefix 'jetty' in the current project and in the plugin groups的问题解决
只需在maven的setting.xml文件上加入如下节点: <pluginGroups> <pluginGroup>org.mortbay.jetty</pluginG ...
- 解决vagrant不能正常挂载目录的问题
解决方案: vagrant plugin install vagrant-vbguest
- Linux 的计划任务(运维基础|可用于提权)
Linux操作系统定时任务系统 Cron 入门 先写笔记: crontab -u //设定某个用户的cron服务,一般root用户在执行这个命令的时候需要此参数 crontab -l //列出某个用户 ...
- csdn开源夏令营-ospaf中期报告
1.背景 随着将中期的代码托管到CSDN的平台上,ospaf(开源项目成熟度分析工具)已经有了小小的雏形.当然还远远不够. 首先还是要感谢这次活动组织方CSDN,感觉挺有G ...
- jquery 创建jquery的dom对象---------------获取自身的html节点及其子节点的html
1.var domObj = $("<dom>"); 2.var a = $("<a href='www.baidu.com'>"); ...
- SharePoint 2013 对话框
The quick way to open a sharepoint 2013 dialog modal form is via Javascript below 1 2 3 4 5 function ...