HDU 5658 CA Loves Palindromic (回文树)
来源:互联网 发布:套路网络用语 编辑:程序博客网 时间:2024/06/06 03:36
CA Loves Palindromic
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 374 Accepted Submission(s): 161
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.
1≤T≤10, 1≤length≤1000, 1≤Q≤100000, 1≤l≤r≤length
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
In second query, the palindromic substrings in the substring
Note that the substring “b” appears twice, but only be counted once.
You may need an input-output optimization.
Source
BestCoder Round #78 (div.2)
Recommend
wange2014 | We have carefully selected several similar problems for you: 5910 5909 5908 5907 5906
回文树。给出的串总长度只有1000,问任意区间中本质不同的回文串数量。因为长度才1000就暴力枚举区间即可。
#include "cstring"#include "cstdio"#include "string.h"#include "iostream"using namespace std;const int MAXN = 1005 ;const int N = 26 ;char str[1005];struct Palindromic_Tree { int next[MAXN][N] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成 int fail[MAXN] ;//fail指针,失配后跳转到fail指针指向的节点 int cnt[MAXN] ; int num[MAXN] ; int len[MAXN] ;//len[i]表示节点i表示的回文串的长度 int S[MAXN] ;//存放添加的字符 int last ;//指向上一个字符所在的节点,方便下一次add int n ;//字符数组指针 int p ;//节点指针 int newnode ( int l ) {//新建节点 for ( int i = 0 ; i < N ; ++ i ) next[p][i] = 0 ; cnt[p] = 0 ; num[p] = 0 ; len[p] = l ; return p ++ ; } void init () {//初始化 p = 0 ; newnode ( 0 ) ; newnode ( -1 ) ; last = 0 ; n = 0 ; S[n] = -1 ;//开头放一个字符集中没有的字符,减少特判 fail[0] = 1 ; } int get_fail ( int x ) {//和KMP一样,失配后找一个尽量最长的 while ( S[n - len[x] - 1] != S[n] ) x = fail[x] ; return x ; } void add ( int c ) { c -= 'a' ; S[++ n] = c ; int cur = get_fail ( last ) ;//通过上一个回文串找这个回文串的匹配位置 if ( !next[cur][c] ) {//如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串 int now = newnode ( len[cur] + 2 ) ;//新建节点 fail[now] = next[get_fail ( fail[cur] )][c] ;//和AC自动机一样建立fail指针,以便失配后跳转 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] ; //父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串! }}tree;int main(){ int cas; scanf("%d",&cas); while(cas--) { long long ans[1005][1005]; //Palindromic_Tree tree; scanf("%s",str+1); tree.init(); int len=strlen(str+1); for(int i=1;i<=len;i++) { tree.init(); for(int j=i;j<=len;j++) { tree.add(str[j]); ans[i][j]=tree.p-2; } } int cnt; scanf("%d",&cnt); while(cnt--) { int l,r; scanf("%d%d",&l,&r); printf("%lld\n",ans[l][r]); } }}
- 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
- 解析public static void main(String[] args)
- 剑指offer2-重建二叉树
- 面向对象_final关键字引入
- Java新特性:函数式接口
- 解除 GPU 进程(减少显卡占用)
- HDU 5658 CA Loves Palindromic (回文树)
- Oracle中的序列(sequence、自增字段)
- 欢迎使用CSDN-markdown编辑器
- spark Reparation和Coalesce 区别
- spring注解的好处
- 1.开发模式接入(微信公众号开发实战)
- JMS基础
- Eclipse中关于maven配置文件setting的一点学习
- Cocos2d-x3.8.1网络编程(Socket)