The Problem to Slow Down You

输入:t个测试样例,每个样例输入两个字符串

输出:这两对字符串的回文串可以组成多少对本质不同的回文串

题意:给你两个字符串,然后问你这两字符串中 有多少对本质不同的字符串子序列

#include<iostream>
#include<stdio.h>
#include <algorithm>
#include <string>
#include<string.h>
#include<math.h>
#define ll long long
using namespace std;
const int MAXN = ;
const int N = ;
char s[MAXN],ss[MAXN];//输入的要处理的字符串
ll ans=;
struct Palindromic_Tree
{
int next[MAXN][] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成
int fail[MAXN] ;//fail指针,失配后跳转到fail指针指向的节点 //回文树里面的一个节点就代表一个回文串 int cnt[MAXN] ;//表示第i个节点代表的回文串出现的次数
int num[MAXN] ; //表示以节点i表示的最长回文串的最右端点为回文串结尾的回文串个数。
int len[MAXN] ;//表示第i个节点代表的回文串长度 int S[MAXN] ;//存放添加的字符
int last ;//指向上一个字符所在的节点,方便下一次add
int n ;//字符数组指针
int p ;//节点指针
int newnode(int l) //在树中新建节点
{
for(int i = ; i < N ; ++ i) next[p][i] = ;
cnt[p] = ;
num[p] = ;
len[p] = l ;
return p ++ ;
}
void init() //初始化
{
p = ;
newnode() ;//建一棵保存长度为偶数的回文树
newnode(-) ;//长度为奇数的回文树
last = ;
n = ;
S[n] = - ;//开头放一个字符集中没有的字符,减少特判
fail[] = ;
}
int get_fail(int x) //和KMP一样,失配后找一个尽量最长的
{
while(S[n - len[x] - ] != S[n])
x = fail[x] ;
return x ;
}
void add(int c,int pos)
{
//printf("%d:",p);//------------>输出节点编号,第一个有回文串的编号时从2开始
c -= 'a';
S[++ n] = c ;
int cur = get_fail(last) ; //通过上一个回文串找这个回文串的匹配位置
//printf("%d ",cur);//输出节点编号p代表的回文串的字符长度
if(!next[cur][c]) //如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串
{
int now = newnode(len[cur] + ) ; //新建节点
fail[now] = next[get_fail(fail[cur])][c] ; //和AC自动机一样建立fail指针,以便失配后跳转
next[cur][c] = now ;
num[now] = num[fail[now]] + ;
//for(int i=pos-len[now]+1; i<=pos; ++i)//--------->输出编号为P代表的回文串
// printf("%c",s[i]);
}
last = next[cur][c] ;
cnt[last] ++ ;
//putchar(10);//------------->输出回车换行
}
void count()
{
for(int i = p - ; i >= ; -- i)
cnt[fail[i]] += cnt[i] ;
//父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!
}
} pat1,pat2;
void dfs(int x,int y)
{
for(int i=;i<;i++)
{
//son1、son2是节点编号
int son1=pat1.next[x][i];
int son2=pat2.next[y][i];
//cout<<son1<<" "<<son2<<endl;
if(son1&&son2)
{
ans=ans+(ll)pat1.cnt[son1]*pat2.cnt[son2];//ans记录有多少对本质不同的字符串子序列
dfs(son1,son2);
}
}
}
int main()
{
int t,k=;
scanf("%d",&t);
while(t--)
{
ans=;
scanf("%s",s);
int n=strlen(s);
pat1.init();
for(int i=; i<n; i++)
pat1.add(s[i],i);
pat1.count();
scanf("%s",&ss);
int nn=strlen(ss);
pat2.init();
for(int i=;i<nn;i++)
pat2.add(ss[i],i);
pat2.count();
dfs(,);//遍历长度为偶数的回文树
dfs(,);//遍历长度为奇数的回文树
printf("Case #%d: ",k++);
printf("%lld\n",ans);
}
return ;
}
/*
3
abacab
abccab
faultydogeuniversity
hasnopalindromeatall
abbacabbaccab
youmayexpectedstrongsamplesbutnow
*/

The Problem to Slow Down You的更多相关文章

  1. 2014-2015 ACM-ICPC, Asia Xian Regional Contest G The Problem to Slow Down You 回文树

    The Problem to Slow Down You Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjud ...

  2. 回文自动机 + DFS --- The 2014 ACM-ICPC Asia Xi’an Regional Contest Problem G.The Problem to Slow Down You

    The Problem to Slow Down You Problem's Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.actio ...

  3. CodeForcesGym 100548G The Problem to Slow Down You

    The Problem to Slow Down You Time Limit: 20000ms Memory Limit: 524288KB This problem will be judged ...

  4. UVALive - 7041 The Problem to Slow Down You (回文树)

    https://vjudge.net/problem/UVALive-7041 题意 给出两个仅包含小写字符的字符串 A 和 B : 求:对于 A 中的每个回文子串,B 中和该子串相同的子串个数的总和 ...

  5. UVAlive 7041 The Problem to Slow Down You(回文树)

    题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  6. Codeforces.GYM100548G.The Problem to Slow Down You(回文树)

    题目链接 \(Description\) 给定两个串\(S,T\),求两个串有多少对相同回文子串. \(|S|,|T|\leq 2\times 10^5\). \(Solution\) 好菜啊QAQ ...

  7. 2014-2015 ACM-ICPC, Asia Xian Regional Contest GThe Problem to Slow Down You

    http://blog.csdn.net/u013368721/article/details/42100363  回文树 建立两棵回文树,然后count处理一遍就可以了,然后顺着这两棵树的边走下去就 ...

  8. Gym - 100548G The Problem to Slow Down You

    依然是回文树. 我们只需要吧siz[]改成统计两边的siz[][0/1],然后把两个字符中间随便加一个不会出现的字符拼起来,做一遍回文树统计一下就OJBK了 #include<bits/stdc ...

  9. UVALive - 7041 G - The Problem to Slow Down You

    题意:求两个串的公共回文子串个数 题解:建两个回文自动机,从0和1各跑一边就是答案了,因为对于回文自动机来说,从头开始dfs就能找出该字符串的所有回文串 //#pragma GCC optimize( ...

随机推荐

  1. 【MySQL】外键的变种

    " 目录 三种关系 多对一 多对多 一对一 因为有foreign key的约束,使得两张表形成了三种关系: 多对一 多对多 一对多 重点理解如何找出两张表之间的关系 现在有A.B两张表 分析 ...

  2. 洛谷 P1043 数字游戏(区间dp)

    题目链接:https://www.luogu.com.cn/problem/P1043 这道题与石子合并很类似,都是把一个环强制改成一个链,然后在链上做区间dp 要初始化出1~2n的前缀和,方便在O( ...

  3. phpRedis函数使用总结【分类详细】

    <?php /*1.Connection*/ $redis = new Redis(); $redis->connect('127.0.0.1',6379,1);//短链接,本地host, ...

  4. websocket 无法查看Data

    websocket 是浏览器新的信息传输协议,记录一些遇到的问题: 调试相关: websocket 连接以后可以在Chrome tools 中的network下看到,如图 要查看通信内容可以选中点击f ...

  5. POJ3264 Balances Lineup

    建两颗线段树分别存最大和最小值,模板题~ #include<cstdio> #include<algorithm> #include<cstring> using ...

  6. 一个含有Fibonacci Number的级数

    \[\Large\displaystyle \sum_{n=0}^\infty \frac{1}{F_{2n+1}+1}=\frac{\sqrt5}{2}\] \(\Large\mathbf{Proo ...

  7. 思科Catalyst 9K

    思科的新一代产品Catalyst9K,里面涉及了Catalyst9200.Catalyst9300.Catalyst9400.Catalyst9500.Catalyst9600和Catalyst980 ...

  8. 杭电 2097 sky数

    Sky数 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  9. 遍历 Map 的方式

    今天获取到一个Map 集合,想循环遍历出内容,突然发现忘记如何遍历Map,平时用的太少. Map 集合的内容是 Key , Value 键值对形式存储 第一种是 for 循环遍历 Map<Str ...

  10. Codeforces Round #616 (Div. 2) D

    莫队的模板 struct node{ int l,r,id; }q[maxn]; int cmp(node a,node b) { ) ? a.r < b.r : a.r > b.r); ...