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的更多相关文章
- 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 ...
- 回文自动机 + 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 ...
- 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 ...
- UVALive - 7041 The Problem to Slow Down You (回文树)
https://vjudge.net/problem/UVALive-7041 题意 给出两个仅包含小写字符的字符串 A 和 B : 求:对于 A 中的每个回文子串,B 中和该子串相同的子串个数的总和 ...
- UVAlive 7041 The Problem to Slow Down You(回文树)
题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- Codeforces.GYM100548G.The Problem to Slow Down You(回文树)
题目链接 \(Description\) 给定两个串\(S,T\),求两个串有多少对相同回文子串. \(|S|,|T|\leq 2\times 10^5\). \(Solution\) 好菜啊QAQ ...
- 2014-2015 ACM-ICPC, Asia Xian Regional Contest GThe Problem to Slow Down You
http://blog.csdn.net/u013368721/article/details/42100363 回文树 建立两棵回文树,然后count处理一遍就可以了,然后顺着这两棵树的边走下去就 ...
- Gym - 100548G The Problem to Slow Down You
依然是回文树. 我们只需要吧siz[]改成统计两边的siz[][0/1],然后把两个字符中间随便加一个不会出现的字符拼起来,做一遍回文树统计一下就OJBK了 #include<bits/stdc ...
- UVALive - 7041 G - The Problem to Slow Down You
题意:求两个串的公共回文子串个数 题解:建两个回文自动机,从0和1各跑一边就是答案了,因为对于回文自动机来说,从头开始dfs就能找出该字符串的所有回文串 //#pragma GCC optimize( ...
随机推荐
- Linux修改本机/etc/hosts的hostName后经常不生效
1.Linux修改本机别名/etc/hosts的hostName后经常不生效解决 Linux修改本机别名/etc/hosts的hostName后经常不生效, 比如我们/etc/hosts的内容如下: ...
- STM32内部时钟树
1.外部晶振是干什么用的? 2.内部晶振是干什么用的? 3.外部晶振频率的大小能影响什么?
- Sqlmap 工具用法详解
Sqlmap 工具用法详解 sqlmap是一款自动化的sql注入工具. 1.主要功能:扫描.发现.利用给定的url的sql注入漏 ...
- 服务器(2)——IIS(2)——IIS Express(1)——IIS跟IIS Express之间的区别和关系
在早期开发.NET WEB应用的时候,是需要为应用项目配置一个IIS下的虚拟应用(VS会自动配置,也可以手工指定),但这个要求操作系统必须支持IIS并且安装IIS(WINDOWS 7 HOME版本是不 ...
- 在java的静态方法中访问类的实例成员
直接来看代码: public class Example { int x = 3;//类的实例变量,初始化值为3 static int y = 4;//类的静态变量,初始化值为4 public sta ...
- 修改oracle数据库用户密码的方法
WIN+R打开运行窗口,输入cmd进入命令行: 输入sqlplus ,输入用户名,输入口令(如果是超级管理员SYS的话需在口令之后加上as sysdba)进入sql命令行: 连接成功后,输入“s ...
- CSS创意与视觉表现
视觉效果 CSS代码: .cover { padding: 36% 50%; background: linear-gradient(to right, white 50%, black calc(5 ...
- IDEA中常用优化设置
1.设置鼠标悬浮提示 Editor->General 这里要勾选下,后面设置的是延迟时间 默认半秒:设置后,我们鼠标移动到类上看看: 2.显示方法分隔符 Editor->General - ...
- LeetCode中等题(三)
题目一: 反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明:1 ≤ m ≤ n ≤ 链表长度. 示例: 输入: 1->2->3->4->5->NULL, m ...
- linux(centos6.10)下去掉mysql的强密码验证
vim /etc/my.cnf shift + G 光标移到最下方: o 进入插入模式,同时换行: 添加一行语句: validate_password=OFF 保存退出. servi ...