HDU5658-CA Loves Palindromic

CA Loves Palindromic

                                                                 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
                                                                                           Total Submission(s): 499    Accepted Submission(s): 216


Problem Description
CA loves strings, especially loves the palindrome strings.
One day he gets a string, he wants to know how many palindromic substrings in the substring  S[l,r] .
Attantion, each same palindromic substring can only be counted once.
 

Input
First line contains  T  denoting the number of testcases.
T  testcases follow. For each testcase:
First line contains a string  S . We ensure that it is contains only with lower case letters.
Second line contains a interger  Q , denoting the number of queries.
Then  Q  lines follow, In each line there are two intergers  l,r , denoting the substring which is queried.
1T10, 1length1000, 1Q100000, 1lrlength
 

Output
For each testcase, output the answer in  Q  lines.
 

Sample Input
  
  
1 abba 2 1 2 1 3
 

Sample Output
  
  
2 3
Hint
In first query, the palindromic substrings in the substring $S[1,2]$ are "a","b". In second query, the palindromic substrings in the substring $S[1,2]$ are "a","b","bb". Note that the substring "b" appears twice, but only be counted once. You may need an input-output optimization.
 

Source
 

Recommend
wange2014


题意:给你一个字符串,每次询问一个区间有多少种回文串

解题思路:回文树预处理出所有区间的答案


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;
const int maxn = 3e5 + 10;
char s[maxn];
int ans[1009][1009], m, l, r;

struct PalindromicTree
{
	const static int maxn = 3e5 + 10;
	int next[maxn][26], last, sz, tot;
	int fail[maxn], len[maxn];
	char s[maxn];
	void Clear()
	{
		len[1] = -1; len[2] = 0;
		fail[2] = fail[1] = 1;
		last = (sz = 3) - 1;
		tot = 0;
		memset(next[1], 0, sizeof(next[1]));
		memset(next[2], 0, sizeof(next[2]));
	}
	int Node(int length)
	{
		memset(next[sz], 0, sizeof(next[sz]));
		len[sz] = length;
		return sz++;
	}
	int getfail(int x)
	{
		while (s[tot] != s[tot - len[x] - 1]) x = fail[x];
		return x;
	}
	int add(char pos)
	{
		int x = (s[++tot] = pos) - 'a', y = getfail(last);
		if (next[y][x]) { last = next[y][x]; return 0; }
		last = next[y][x] = Node(len[y] + 2);
		fail[last] = len[last] == 1 ? 2 : next[getfail(fail[y])][x];
		return 1;
	}
}solve;

int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%s", s);
		for (int i = 0; s[i]; i++)
		{
			solve.Clear();
			ans[i + 1][i] = 0;
			for (int j = i; s[j]; j++)
				ans[i + 1][j + 1] = ans[i + 1][j] + solve.add(s[j]);
		}
		scanf("%d", &m);
		while (m--)
		{
			scanf("%d%d", &l, &r);
			printf("%d\n", ans[l][r]);
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值