HDU 5658 CA Loves Palindromic
来源:互联网 发布:android软件下载 编辑:程序博客网 时间:2024/05/16 12:01
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 substringS[l,r] .
Attantion, each same palindromic substring can only be counted once.
One day he gets a string, he wants to know how many palindromic substrings in the substring
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 stringS . We ensure that it is contains only with lower case letters.
Second line contains a intergerQ , denoting the number of queries.
ThenQ lines follow, In each line there are two intergers l,r , denoting the substring which is queried.
1≤T≤10, 1≤length≤1000, 1≤Q≤100000, 1≤l≤r≤length
First line contains a string
Second line contains a interger
Then
Output
For each testcase, output the answer in Q lines.
Sample Input
1abba21 21 3
Sample Output
23HintIn 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.
给定一个字符串,求给定区间的本质不同的回文子串,直接利用回文树即可。
#include<map>#include<set>#include<cmath>#include<queue>#include<stack>#include<bitset>#include<cstdio>#include<string>#include<cstring>#include<algorithm>#include<functional>using namespace std;typedef long long LL;const int low(int x) { return x&-x; }const int INF = 0x7FFFFFFF;const int mod = 1e9 + 7;const int maxn = 1e3 + 10;int T, n, ans[maxn][maxn], l, r;char s[maxn];struct PalindromicTree{const static int maxn = 1e5 + 10;const static int size = 26;int next[maxn][size], last, sz, tot;int fail[maxn], len[maxn], cnt[maxn];char s[maxn];void clear() { len[1] = -1; len[2] = 0;fail[2] = fail[1] = 1;last = (sz = 3) - 1;cnt[1] = cnt[2] = 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[sz] = len[sz] == 1 ? 2 : next[getfail(fail[y])][x];return sz++, 1;}}solve;int main(){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", &n);while (n--){scanf("%d%d", &l, &r);printf("%d\n", ans[l][r]);}}return 0;}
0 0
- HDU 5658 CA Loves Palindromic
- HDU 5658 CA Loves Palindromic (回文树)
- HDU 5658 CA Loves Palindromic(回文树)
- HDU 5658 CA Loves Palindromic (回文树。)
- HDU5658-CA Loves Palindromic
- HDU - CA Loves GCD
- CA Loves Stick HDU
- CA Loves Stick HDU
- BestCoder78 DIV1 T3 &&hdu5658 CA Loves Palindromic
- HDU 5655 CA Loves Stick
- HDU 5656:CA Loves GCD
- HDU 5655 CA Loves Stick
- HDU 5656 CA Loves GCD
- HDU 5656 CA Loves GCD
- HDU - 5655 CA Loves Stick
- hdu CA Loves GCD dp
- hdu 5656 CA Loves GCD
- HDU 5655 CA Loves Stick
- 在设置浏览器访问runtime下的临时文件的路径前缀,完整的
- nodemanager节点报错Unexpected error starting NodeStatusUpdater
- 【转】PC机安装MAC虚拟机
- Python的readlines(sizeint)
- 笔记(4/11-4/12) java基础
- HDU 5658 CA Loves Palindromic
- aapt.exe'' finished with non-zero exit value 1错误
- Adobe Acrobat 9重新激活
- 怎样制作EDIUS 8中的色块
- 3.4 黑盒测试用例的设计方法 之 等价类划分与边界值分析
- 无聊·无知
- Leaf编程问题汇总#1
- C语言中的static函数,C++中的静态成员和静态成员函数
- 庸人