HDU 3948 The Number of Palindromes

原创 2016年08月28日 17:20:53

Description

Now, you are given a string S. We want to know how many distinct substring of S which is palindrome.

Input

The first line of the input contains a single integer T(T<=20), which indicates number of test cases. 
Each test case consists of a string S, whose length is less than 100000 and only contains lowercase letters. 

Output

For every test case, you should output "Case #k:" first in a single line, where k indicates the case number and starts at 1. Then output the number of distinct substring of S which is palindrome. 

Sample Input

3
aaaa
abab
abcd

Sample Output

Case #1: 4
Case #2: 4

Case #3: 4

求本质不同的回文子串的数目,显然是用回文树搞定最轻松了,这里为了是练习后缀数组。

#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define fi first
#define se second
#define mp(i,j) make_pair(i,j)
#define pii pair<string,string>
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF;
const int mod = 1e8;
const int N = 5e5 + 10;
const int read()
{
	char ch = getchar();
	while (ch<'0' || ch>'9') ch = getchar();
	int x = ch - '0';
	while ((ch = getchar()) >= '0'&&ch <= '9') x = x * 10 + ch - '0';
	return x;
}
int T, cas = 0;

struct Sa
{
	char s[N];
	int rk[2][N], sa[N], h[N], w[N], now, n;
	int rmq[N][20], lg[N];

	bool GetS()
	{
		scanf("%s", s + 1);
		n = strlen(s + 1); 
		s[n + 1] = 2;
		rep(i, 1, n) s[n + n + 2 - i] = s[i];
		n = n + n + 1;
		return true;
	}

	void getsa(int z, int &m)
	{
		int x = now, y = now ^= 1;
		rep(i, 1, z) rk[y][i] = n - i + 1;
		for (int i = 1, j = z; i <= n; i++)
			if (sa[i] > z) rk[y][++j] = sa[i] - z;

		rep(i, 1, m) w[i] = 0;
		rep(i, 1, n) w[rk[x][rk[y][i]]]++;
		rep(i, 1, m) w[i] += w[i - 1];
		per(i, n, 1) sa[w[rk[x][rk[y][i]]]--] = rk[y][i];
		for (int i = m = 1; i <= n; i++)
		{
			int *a = rk[x] + sa[i], *b = rk[x] + sa[i - 1];
			rk[y][sa[i]] = *a == *b&&*(a + z) == *(b + z) ? m - 1 : m++;
		}
	}

	void getsa(int m)
	{
		//n = strlen(s + 1);
		rk[1][0] = now = sa[0] = s[0] = 0;
		rep(i, 1, m) w[i] = 0;
		rep(i, 1, n) w[s[i]]++;
		rep(i, 1, m) rk[1][i] = rk[1][i - 1] + (bool)w[i];
		rep(i, 1, m) w[i] += w[i - 1];
		rep(i, 1, n) rk[0][i] = rk[1][s[i]];
		rep(i, 1, n) sa[w[s[i]]--] = i;

		rk[1][n + 1] = rk[0][n + 1] = 0;	//多组的时候容易出bug
		for (int x = 1, y = rk[1][m]; x <= n && y <= n; x <<= 1) getsa(x, y);
		for (int i = 1, j = 0; i <= n; h[rk[now][i++]] = j ? j-- : j)
		{
			if (rk[now][i] == 1) continue;
			int k = n - max(sa[rk[now][i] - 1], i);
			while (j <= k && s[sa[rk[now][i] - 1] + j] == s[i + j]) ++j;
		}
	}

	void getrmq()
	{
		h[n + 1] = h[1] = lg[1] = 0;
		rep(i, 2, n) rmq[i][0] = h[i], lg[i] = lg[i >> 1] + 1;
		for (int i = 1; (1 << i) <= n; i++)
		{
			rep(j, 2, n)
			{
				if (j + (1 << i) > n + 1) break;
				rmq[j][i] = min(rmq[j][i - 1], rmq[j + (1 << i - 1)][i - 1]);
			}
		}
	}

	int lcp(int x, int y)
	{
		int l = min(rk[now][x], rk[now][y]) + 1, r = max(rk[now][x], rk[now][y]);
		return min(rmq[l][lg[r - l + 1]], rmq[r - (1 << lg[r - l + 1]) + 1][lg[r - l + 1]]);
	}

	void work()
	{
		GetS();	getsa(256); getrmq();
		int ans = 0, odd = 0, even = 0;
		rep(i, 1, n)
		{
		    odd = min(odd, h[i]), even = min(even, h[i]);
			if (sa[i] > n / 2) continue;
			int L = lcp(sa[i], n + 1 - sa[i]);
			ans += max(0, L - odd);  odd = max(L, odd);
			int R = sa[i] == 1 ? 0 : lcp(sa[i], n + 2 - sa[i]);
			ans += max(0, R - even); even = max(R, even);
		}
		printf("Case #%d: %d\n", ++cas, ans);
	}
}sa;

int main()
{
	T = read(); while (T--) sa.work();
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

HDU 3948 The Number of Palindromes

Description Now, you are given a string S. We want to know how many distinct substring of S which...

HDU 3948 The Number of Palindromes(后缀数组)

题意:求一个串中不相同回文子串的个数。 做法:本来想用manacher。但是无法判重。只能利用后缀数组,按照求回文串常规做法,把原串翻转拼到原串后面,求出height,以及利用sparse tabl...

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

HDU - 3948 The Number of Palindromes 后缀数组+ST表、distinct substring

题意:给出一个字符串,求出其回文子串的种数。 后缀数组+ST表 把s变成 s + '#' + s',其中s'表示s的反向串。 所以当回文子串的长度为奇数时 [i+1, _rank[n - sa[i...

hdu 3948 The Number of Palindromes 求字符串不重复回文子串个数 后缀数组

Problem Description Now, you are given a string S. We want to know how many distinct substring of...

HDU 1293 The Number of Paths

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1293 Problem Description Let f (n) be t...

CodeForces - 245H Queries for Number of Palindromes

You’ve got a string s = s1s2… s|s| of length |s|, consisting of lowercase English letters. There als...

HDU 4727-The Number Off of FFF

X soldiers from the famous “FFF army” is standing in a line, from left to right.

Codeforces 245H Queries for Number of Palindromes

很简单的回文 dp[i][j]表示从str[i

SPOJ Number of Palindromes(回文树)

SPOJ Number of Palindromes(回文树)

AUG13-The Number Of Solutions

Little Chief loves math. Most of all, he loves equations. He can solve any equation in the whole wor...
  • Taceo
  • Taceo
  • 2013-08-06 21:17
  • 707
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)