2014-2015 ACM-ICPC, Asia Xian Regional Contest G

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. A
p..q = Bs..t
3. A
p..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.
12
The 2014 ACM-ICPC Asia Xi’an Regional Contest October 26, 2014
Samples
Sample Input Sample Output
3
abacab
abccab
faultydogeuniversity
hasnopalindromeatall
abbacabbaccab
youmayexpectedstrongsamplesbutnow
Case #1: 12
Case #2: 20

Case #3: 18


题意:

给出两个字符串A,B,询问有多少对a的子串Sa,b的子串Sb,使得 Sa == Sb && Sa是回文串


solution:

首先构建一棵A的回文树

参考blog

考虑从左往右逐个插入B的每个字符

我们在这棵回文树找到当前第pos个字符在回文树中能对应的长度最长的节点

找不到就跳过

则显然,这个位置的答案是从这个点出发,不断走fail边的cnt数组的求和

通过BFS维护一个数组就好


一开始计数方式出了些差错。。GG

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;

const int maxn = 2E5 + 20;
const int Size = 26;
typedef long long LL;

int tot,T,tail,now,ch[maxn][Size],cnt[maxn],fail[maxn],len[maxn],du[maxn],d2[maxn];
char A[maxn],B[maxn],S[maxn];
LL ans[maxn];

queue <int> Q;
vector <int> v[maxn];
vector <int> v2[maxn];

void Build_tree(int Len)
{
	tail = now = 0;
	tot = 1;
	len[0] = 0; 
	len[1] = -1;
	fail[0] = 1;
	cnt[0] = cnt[1] = 0;
	ans[0] = ans[1] = 0;
	memset(ch[0],0,sizeof(ch[0]));
	memset(ch[1],0,sizeof(ch[1]));
	for (int i = 1; i <= Len; i++) {
		S[++tail] = A[i];
		while (S[tail - len[now] - 1] != S[tail])
			now = fail[now];
		int Nex = A[i] - 'a';
		if (!ch[now][Nex]) {
			ch[now][Nex] = ++tot;
			cnt[tot] = ans[tot] = 0;
			memset(ch[tot],0,sizeof(ch[tot]));
			len[tot] = len[now] + 2;
			if (len[tot] > 1) {
				int cur = fail[now];
				while (S[tail - len[cur] - 1] != S[tail])
					cur = fail[cur];
				fail[tot] = ch[cur][Nex];
			}
			else fail[tot] = 0;
			++du[fail[tot]];
			v[tot].push_back(fail[tot]);
			++d2[tot];
			v2[fail[tot]].push_back(tot);
		}
		now = ch[now][Nex];
		++cnt[now];
	}
}

void BFS()
{
	for (int i = 0; i <= tot; i++)
		if (!du[i])
			Q.push(i);
	while (!Q.empty()) {
		int k = Q.front(); Q.pop();
		for (int i = 0; i < v[k].size(); i++) {
			int fa = v[k][i];
			cnt[fa] += cnt[k];
			--du[fa];
			if (!du[fa]) Q.push(fa);
		}
	}
	for (int i = 0; i <= tot; i++) v[i].clear();
	
	for (int i = 0; i <= tot; i++)
		if (!d2[i])
			Q.push(i);
	cnt[0] = cnt[1] = 0;
	while (!Q.empty()) {
		int k = Q.front(); Q.pop();
		ans[k] += 1LL*cnt[k];
		for (int i = 0; i < v2[k].size(); i++) {
			int to = v2[k][i];
			ans[to] += ans[k];
			--d2[to];
			if (!d2[to]) Q.push(to);
		}
	}
	for (int i = 0; i <= tot; i++) v2[i].clear();
	ans[0] = ans[1] = 0;
}

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
	#endif
	
	cin >> T;
	for (int I = 1; I <= T; I++) {
		scanf("%s",A + 1);
		int Len = strlen(A + 1);
		Build_tree(Len);
		
		BFS();
		scanf("%s",B + 1);
		LL Ans = 0;
		now = 0;
		Len = strlen(B + 1);
		for (int i = 1; i <= Len; i++) {
			int Nex = B[i] - 'a';
			while (B[i - len[now] - 1] != B[i] || !ch[now][Nex]) {
				if (now == 1) break;
				now = fail[now];
			}
			now = ch[now][Nex];
			Ans += ans[now];
		}
		printf("Case #%d: %I64d\n",I,Ans);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值