2014 Asia Xian Regional Contest G The Problem to Slow Down You 回文串自动机
来源:互联网 发布:淘宝网摩托车 编辑:程序博客网 时间:2024/05/17 01:07
对两个字符串分别建回文串自动机就可以了,Hash记录一下每个回文串出现了多少次。
坑爹的是这题卡自然溢出的hash,没办法只好多模一个数。。。。
出题人太邪恶了。。。。
Problem G. The Problem to Slow Down You
Description
After finishing his homework, our problem setter Federmann decided to kill time by hanging
around online. He found a cool chat room that discusses competitive programming. Federmann
has already joined lot of such chat rooms, but this one is special. Once he entered the
chat room, he noticed that there is an announcement saying “We forbid off-topic messages!”.
Federmann thinks that’s quite unusual, he decided to sit down and join the talk. After watching
people discussing different programming challenges for a while, he found an interesting
message saying “No, Federmann won’t prepare another problem about strings this year.”
“Oh, why do you guys think about that?” Federmann smiled. “Don’t they know I have
an Edward number2 of 3?”
He then thought about something about palindrome, given two strings A and B, what
is the number of their common palindrome substrings? The amount of common palindrome
substrings between two strings is defined as the number of quadruple (p, q, s, t), which satisfies
that:
1. 1 ≤ p, q ≤ length(A), 1 ≤ s, t ≤ length(B), p ≤ q and s ≤ t. Here length(A) means the
length of string A.
2. Ap..q = Bs..t
3. Ap..q is palindrome. (palindrome string is the string that reads the same forward or
backward)
For example, (1, 3, 1, 3) and (1, 3, 3, 5) are both considered as a valid common palindrome
substring between aba and ababa.
Federmann is excited about his new task, and he is just too lazy to write solutions, help
him.
Input
The first line of the input gives the number of test cases, T. T test cases follow. For each
test case, the first line contains a string A and the second line contains a string B. The length
of A, B will not exceed 200000.
It is guaranteed the input file will be smaller than 8 MB.
Output
For each test case, output one line containing “Case #x: y”, where x is the test case
number (starting from 1) and y is the number of common palindrome substrings of A and B.
2The Edward number is something like Erdős number, among problem setters.
Samples
Sample Input Sample Output
3
abacab
abccab
faultydogeuniversity
hasnopalindromeatall
abbacabbaccab
youmayexpectedstrongsamplesbutnow
Case #1: 12
Case #2: 20
Case #3: 18
/* ***********************************************Author :CKbossCreated Time :2015年04月18日 星期六 08时35分11秒File Name :XianG.cpp************************************************ */#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <set>#include <map>using namespace std;typedef unsigned long long int ull;const ull X=127;const ull Mod=19239901;const int maxn=220000;const int C=30;int next[maxn][C];int fail[maxn],num[maxn],len[maxn];ull cnt[maxn];int s[maxn];int last,p,n;int newnode(int x){ for(int i=0;i<C;i++) next[p][i]=0; cnt[p]=0; num[p]=0; len[p]=x; return p++;}void init(){ p=0; newnode(0); newnode(-1); last=0; n=0; s[0]=-1; fail[0]=1;}int getfail(int x){ while(s[n-len[x]-1]!=s[n]) x=fail[x]; return x;}void add(int c){ c-='a'; s[++n]=c; int cur=getfail(last); if(!next[cur][c]) { int now=newnode(len[cur]+2); fail[now]=next[getfail(fail[cur])][c]; next[cur][c]=now; num[now]=num[fail[now]]+1; } last=next[cur][c]; cnt[last]++;}void count(){ for(int i=p-1;i>=0;i--) cnt[fail[i]]+=cnt[i];}char str1[maxn],str2[maxn];int n1,n2;struct HaHa{ ull hash1,hash2; bool operator<(const HaHa& b) const { if(hash1!=b.hash1) return hash1<b.hash1; return hash2<b.hash2; }};map<HaHa,ull> mui;struct Point{ int id; HaHa ha;};void BFS1(){ queue<Point> q; q.push((Point){0,(HaHa){30,30}}); // even root q.push((Point){1,(HaHa){31,31}}); // odd root while(!q.empty()) { Point u=q.front(),v; q.pop(); for(int i=0;i<26;i++) { if(next[u.id][i]!=0) { v.id=next[u.id][i]; v.ha.hash1=u.ha.hash1*X+(i+1); v.ha.hash2=(u.ha.hash2*X+(i+1))%Mod; mui[v.ha]=cnt[v.id]; q.push(v); } } }}ull ans;void BFS2(){ ans=0; queue<Point> q; q.push((Point){0,(HaHa){30,30}}); // even root q.push((Point){1,(HaHa){31,31}}); // odd root while(!q.empty()) { Point u=q.front(),v; q.pop(); for(int i=0;i<26;i++) { if(next[u.id][i]!=0) { v.id=next[u.id][i]; v.ha.hash1=u.ha.hash1*X+(i+1); v.ha.hash2=(u.ha.hash2*X+(i+1))%Mod; ans+=cnt[v.id]*mui[v.ha]; q.push(v); } } }}int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int T_T,cas=1; cin>>T_T; while(T_T--) { scanf("%s%s",str1,str2); n1=strlen(str1); n2=strlen(str2); init(); for(int i=0;i<n1;i++) add(str1[i]); count(); mui.clear(); BFS1(); init(); for(int i=0;i<n2;i++) add(str2[i]); count(); BFS2(); cout<<"Case #"<<cas++<<": "<<ans<<endl; } return 0;}
- 2014 Asia Xian Regional Contest G The Problem to Slow Down You 回文串自动机
- Codeforce 2014ACM-ICPC, Asia Xian Regional Contest Problem G. The Problem to Slow Down You(回文树)
- 2014-2015 ACM-ICPC, Asia Xian Regional Contest(G - The Problem to Slow Down You-回文树)
- 【codeforces】2014 Asia Xian Regional Contest G The Problem to Slow Down You 【Palindromic Tree】
- 2014 Asia Xian Regional Contest G The Problem to Slow Down You 【Palindromic Tree】
- 回文自动机 + DFS --- The 2014 ACM-ICPC Asia Xi’an Regional Contest Problem G.The Problem to Slow Down You
- 2014-2015 ACM-ICPC, Asia Xian Regional Contest(H - The Problem to Make You Happy )
- UVA 7041 The Problem to Slow Down You (回文树)
- 【后缀树】acm 2014 西安赛区 Problem G. The Problem to Slow Down You
- Gym100548G-The Problem to Slow Down You
- 2014-2015 ACM-ICPC, Asia Xian Regional Contest G
- [The Problem to Slow Down You] 后缀自动机 + 马拉车做法
- UVAlive 7041 The Problem to Slow Down You(回文树)
- UVALive 7041 The Problem to Slow Down You(回文树)
- 2014-2015 ACM-ICPC, Asia Xian Regional Contest C – The Problem Needs 3D Arrays(最大密度子图)
- Codeforces Gym 100548G The Problem to Slow Down You (Palindromic Tree 或 Hash水过) 2014西安现场赛G题
- 2014-2015 ACM-ICPC, Asia Xian Regional Contest F Color
- 2014-2015 ACM-ICPC, Asia Xian Regional Contest C
- [BZOJ1500]NOI2005 维修数列|splay
- MUD游戏编程 创建TCP数据套接字
- 使用jquery获取url及url参数的方法
- DS之线性表
- 第7周项目3-第9章(怎样使用类和对象)思维导图
- 2014 Asia Xian Regional Contest G The Problem to Slow Down You 回文串自动机
- Unity之megaFierstext翻书插件控制代码分析
- 自定义一个vector向量类
- C# Mysql封装操作类
- 读《MySQL性能调优与架构设计》笔记之MySQL可扩展设计的基本原则
- 一步一步写算法(之hash表)
- 回调函数的理解
- Centos6.5最小安装版本命令行连接无线网络(加密模式为 WPA/WPA2-PSK)
- Installing the SciPy Stack