HDU 5343 MZL's Circle Zhou
MZL's Circle Zhou
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
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的更多相关文章
- 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形成的不 ...
- 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 ...
- HDU5343 MZL's Circle Zhou(SAM+记忆化搜索)
Problem Description MZL's Circle Zhou is good at solving some counting problems. One day, he comes u ...
- [HDU5343]MZL's Circle Zhou
题目大意: 给你两个字符串a和b,从中分别取出子串x和y,求不同的x+y的个数. 思路: 对于每一个字符串,构建SAM. 为了保证相同的x+y不会被重复统计,我们可以想办法只统计相同的x+y中x最长的 ...
- HDU5343:MZL's Circle Zhou(SAM,记忆化搜索DP)
Description Input Output Sample Input Sample Output Solution 题意:给你两个串,分别从两个里面各选出一个子串拼到一起,问能构成多少个本质不同 ...
- 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] ...
- Hdu 5352 MZL's City (多重匹配)
题目链接: Hdu 5352 MZL's City 题目描述: 有n各节点,m个操作.刚开始的时候节点都是相互独立的,一共有三种操作: 1:把所有和x在一个连通块内的未重建过的点全部重建. 2:建立一 ...
- Hdu 5348 MZL's endless loop (dfs)
题目链接: Hdu 5348 MZL's endless loop 题目描述: 给出一个无向图(有环,有重边),包含n个顶点,m条边,问能否给m条边指定方向,使每个顶点都满足abs(出度-入度)< ...
- hdu 5349 MZL's simple problem
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5349 MZL's simple problem Description A simple proble ...
随机推荐
- RAID5配置及服务器2003系统安装方法。2000系统的安装要使用7.9版本的引导盘
服务器2003系统安装方法.2000系统的安装要使用7.9版本的引导盘 RAID5配置: 1.为新到的惠普DL380服务器装系统,首先要配置RAID阵列. 如下图进行配置. 启动到如上图阶段,按F8. ...
- yii2的form表单用法
使用表单 本章节将介绍如何创建一个从用户那搜集数据的表单页.该页将显示一个包含 name 输入框和 email 输入框的表单.当搜集完这两部分信息后,页面将会显示用户输入的信息. 为了实现这个目标,除 ...
- JQuery 总结
JQuery官方网站 http://jquery.com/ 1.JQuery概念 A.Jquery是一个优秀的Javascript框架.它是轻量级的js库 ,它兼容CSS3,还兼容各种浏览器,jQu ...
- ansible剧本
yaml简介 YAML是"YAML Ain't a Markup Language"(YAML不是一种置标语言)的递归缩写,早先YAML的意思其实是:"Yet Anoth ...
- 服务器上oracle的监听设置
1.查看本机的计算机名 2.修改etc/host 3.修改oracle的listener.ora(我服务器上的路径:) 4.修改tnsnames.ora(和上边文件一个目录)
- ACM_变形课(并查集)
变形课 Time Limit: 2000/1000ms (Java/Others) Problem Description: 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermio ...
- hbuilder中的 http://www.w3.org/TR/html4/frameset.dtd
<!-- This is the HTML 4.01 Frameset DTD, which should be used for documents with frames. This DTD ...
- EasyUI系列学习(二)-使用EasyUI
一.引入必要文件 <!--1.0引入jQuery核心库--> <script src="jquery-easyui-1.4.4/jquery.min.js"> ...
- 检查阿里云ssl证书到期情况
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019-06-10 16:00 # @Author : Anthony.long # ...
- JavaScript中的 函数splice() 的使用。
大二接触JavaScript初期,学习函数中有一道题: 定义一个2个参数的函数.第1个参数是一个数组,第2个参数是需要删除的元素.函数功能,在第1个实参数组中查找第2个实参提供的值,找到则删除该元素( ...