MZL's Circle Zhou

Time Limit: 1000ms
Memory Limit: 131072KB

This problem will be judged on HDU. Original ID: 5343
64-bit integer IO format: %I64d      Java class name: Main

 

MZL's Circle Zhou is good at solving some counting problems. One day, he comes up with a counting problem:
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.

Input
The first line of the input is a single integer T (T≤5), indicating the number of testcases.
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.

Output

For each test case, output one line, a single integer indicating the answer.

 

Sample Input

2
acbcc
cccabc
bbbabbababbababbaaaabbbbabbaaaabaabbabbabbbaaabaab
abbaabbabbaaaabbbaababbabbabababaaaaabbaabbaabbaab

Sample Output

135
557539

Source

 
解题:后缀自动机

A、B两串分别建两个SAM处理,先将B串翻转,利用SAM(B)求以字符c结尾(开头)的子串个数。

对于SAM(A)中节点x,若x不能接受字符c,则将B串中以c开头的子串“接”在其后,构成Asub+Bsub的形式。

 #include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ULL;
const int maxn = ;
struct node{
int son[],f,len,alpha;
void init(){
memset(son,-,sizeof son);
f = -;
len = ;
}
};
struct SAM{
node e[maxn];
int tot,last;
int newnode(int len = ,int alpha = ){
e[tot].init();
e[tot].len = len;
e[tot].alpha = alpha;
return tot++;
}
void init(){
tot = last = ;
newnode();
}
void add(int c){
int p = last,np = newnode(e[p].len + ,c);
while(p != - && e[p].son[c] == -){
e[p].son[c] = np;
p = e[p].f;
}
if(p == -) e[np].f = ;
else{
int q = e[p].son[c];
if(e[p].len + == e[q].len) e[np].f = q;
else{
int nq = newnode();
e[nq] = e[q];
e[nq].len = e[p].len + ;
e[np].f = e[q].f = nq;
while(p != - && e[p].son[c] == q){
e[p].son[c] = nq;
p = e[p].f;
}
}
}
last = np;
}
}sam;
char a[maxn],b[maxn];
ULL dp[];
int main(){
int kase;
scanf("%d",&kase);
while(kase--){
scanf("%s%s",a,b);
reverse(b,b + strlen(b));
sam.init();
node *e = sam.e;
memset(dp,,sizeof dp);
for(int i = ; b[i]; ++i)
sam.add(b[i] - 'a');
for(int i = ; i < sam.tot; ++i)
dp[e[i].alpha] += e[i].len - e[e[i].f].len;
sam.init();
ULL ret = ;
for(int i = ; a[i]; ++i)
sam.add(a[i] - 'a');
for(int i = ; i < sam.tot; ++i){
ret += e[i].len - e[e[i].f].len;
for(int j = ; j < ; ++j)
if(e[i].son[j] == -) ret += dp[j]*(e[i].len - e[e[i].f].len);
}
printf("%I64u\n",ret + );
}
return ;
}

HDU 5343 MZL's Circle Zhou的更多相关文章

  1. 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形成的不 ...

  2. HDU 5343 MZL's Circle Zhou 后缀自动机+DP

    MZL's Circle Zhou Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  3. HDU5343 MZL's Circle Zhou(SAM+记忆化搜索)

    Problem Description MZL's Circle Zhou is good at solving some counting problems. One day, he comes u ...

  4. [HDU5343]MZL's Circle Zhou

    题目大意: 给你两个字符串a和b,从中分别取出子串x和y,求不同的x+y的个数. 思路: 对于每一个字符串,构建SAM. 为了保证相同的x+y不会被重复统计,我们可以想办法只统计相同的x+y中x最长的 ...

  5. HDU5343:MZL's Circle Zhou(SAM,记忆化搜索DP)

    Description Input Output Sample Input Sample Output Solution 题意:给你两个串,分别从两个里面各选出一个子串拼到一起,问能构成多少个本质不同 ...

  6. HDU 5351 MZL's Border (规律,大数)

    [HDU 5351 MZL's Border]题意 定义字符串$f_1=b,f_2=a,f_i=f_{i-1}f_{i-2}$. 对$f_n$的长度为$m$的前缀$s$, 求最大的$k$满足$s[1] ...

  7. Hdu 5352 MZL's City (多重匹配)

    题目链接: Hdu 5352 MZL's City 题目描述: 有n各节点,m个操作.刚开始的时候节点都是相互独立的,一共有三种操作: 1:把所有和x在一个连通块内的未重建过的点全部重建. 2:建立一 ...

  8. Hdu 5348 MZL's endless loop (dfs)

    题目链接: Hdu 5348 MZL's endless loop 题目描述: 给出一个无向图(有环,有重边),包含n个顶点,m条边,问能否给m条边指定方向,使每个顶点都满足abs(出度-入度)< ...

  9. hdu 5349 MZL's simple problem

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5349 MZL's simple problem Description A simple proble ...

随机推荐

  1. influxdb入门——和mongodb一样可以动态增加字段

    ./influxd [--config yourconfigfile 2> /dev/null]  之所以重定向 因为默认log是stderr 再启动客户端./influx > CREAT ...

  2. bzoj1023 [SHOI2008]cactus仙人掌图 & poj3567 Cactus Reloaded——求仙人掌直径

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1023    http://poj.org/problem?id=3567 仙人掌!直接模仿 ...

  3. codeforces educational round 25

    A 出题人不给样例解释...具体程序 #include<bits/stdc++.h> using namespace std; int n; ]; int main() { scanf() ...

  4. 91. Ext中获取combobox中的valueField和displayField的值

    转自:https://blog.csdn.net/jcy472578/article/details/42113119Ext.getCmp("schemaVersion").val ...

  5. leetcode快排相关

    leetcode:75颜色分类(3way).215数组中的第K个最大元素(normal) 3way private static void quick3waySort(int[] arr, int l ...

  6. flex中align-content属性

    在flex弹性盒模型中align-content属性控制容器内多行在交叉轴上的排列方式 默认值:stretch 可用值: 属性值:flex-start 属性值:flex-end 属性值:center ...

  7. Oh,mygoddess

    很早的时候就看了这一道题目 , 当时不会做 , 现在 边听歌边写无压力 ........ 题意 : 光辉骑士 一直都在 迷宫的右上角 , 第一行给你迷宫的规格 , 下面是迷宫 "O" ...

  8. JAVA FORK JOIN EXAMPLE--转

    http://www.javacreed.com/java-fork-join-example/ Java 7 introduced a new type of ExecutorService (Ja ...

  9. WinForms_ListView中获取选中项数据值

    string value = listList.SelectedItems[0].SubItems[1].Text;//获取首行listview的值 string va = listList.Sele ...

  10. 单例模式在多线程环境下的lazy模式为什么要加两个if(instance==null)

    刚才在看阿寻的博客”C#设计模式学习笔记-单例模式“时,发现了评论里有几个人在问单例模式在多线程环境下为什么lazy模式要加两个if进行判断,评论中的一个哥们剑过不留痕,给他们写了一个demo来告诉他 ...