CA Loves Palindromic

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Total Submission(s): 301    Accepted Submission(s): 131

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
求区间内的本质不同的回文串的个数
字符串的长度是1000
我们可以利用回文树,求出每个区间内不同回文串的个数
枚举区间
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <stdio.h> using namespace std;
typedef long long int LL;
const int MAX=100000;
const int maxn=1000;
char str[maxn+5];
int sum[maxn+5][maxn+5];
struct Tree
{
int next[MAX+5][26];
int num[MAX+5];
int cnt[MAX+5];
int fail[MAX+5];
int len[MAX+5];
int s[MAX+5];
int p;
int last;
int n;
int new_node(int x)
{
memset(next[p],0,sizeof(next[p]));
cnt[p]=0;
num[p]=0;
len[p]=x;
return p++;
}
void init()
{
p=0;
new_node(0);
new_node(-1);
last=0;
n=0;
s[0]=-1;
fail[0]=1;
}
int get_fail(int x)
{
while(s[n-len[x]-1]!=s[n])
x=fail[x];
return x;
}
int add(int x)
{
x-='a';
s[++n]=x;
int cur=get_fail(last);
if(!(last=next[cur][x]))
{
int now=new_node(len[cur]+2);
fail[now]=next[get_fail(fail[cur])][x];
next[cur][x]=now;
num[now]=num[fail[now]]+1;
last=now;
return 1;
}
cnt[last]++;
return 0;
}
void count()
{
for(int i=p-1;i>=0;p++)
cnt[fail[i]]+=cnt[i];
}
}tree;
int q;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{ scanf("%s",str+1); 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]);
sum[i][j]=tree.p-2;
}
}
scanf("%d",&q);
int l,r;
for(int i=1;i<=q;i++)
{
scanf("%d%d",&l,&r);
printf("%d\n",sum[l][r]);
}
}
return 0;
}

HDU 5658 CA Loves Palindromic(回文树)的更多相关文章

  1. HDU - 5785:Interesting (回文树,求相邻双回文的乘积)

    Alice get a string S. She thinks palindrome string is interesting. Now she wanna know how many three ...

  2. 回文树练习 Part1

    URAL - 1960   Palindromes and Super Abilities 回文树水题,每次插入时统计数量即可. #include<bits/stdc++.h> using ...

  3. HDU5658:CA Loves Palindromic (回文树,求区间本质不同的回文串数)

    CA loves strings, especially loves the palindrome strings. One day he gets a string, he wants to kno ...

  4. 回文树 Palindromic Tree

    回文树 Palindromic Tree 嗯..回文树是个什么东西呢. 回文树(或者说是回文自动机)每个节点代表一个本质不同的回文串. 首先它类似字典树,每个节点有SIGMA个儿子,表示对应的字母. ...

  5. HDU 5421 Victor and String(回文树)

    Victor and String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/262144 K (Java/Othe ...

  6. HDU.5394.Trie in Tina Town(回文树)

    题目链接 \(Description\) 给定一棵\(Trie\).求\(Trie\)上所有回文串 长度乘以出现次数 的和.这里的回文串只能是从上到下的一条链. 节点数\(n\leq 2\times ...

  7. Palindromic Tree 回文自动机-回文树 例题+讲解

    回文树,也叫回文自动机,是2014年被西伯利亚民族发明的,其功能如下: 1.求前缀字符串中的本质不同的回文串种类 2.求每个本质不同回文串的个数 3.以下标i为结尾的回文串个数/种类 4.每个本质不同 ...

  8. ZOJ 3661 Palindromic Substring(回文树)

    Palindromic Substring Time Limit: 10 Seconds      Memory Limit: 65536 KB In the kingdom of string, p ...

  9. HDU - 5421:Victor and String (回文树,支持首尾插入新字符)

    Sample Input 6 1 a 1 b 2 a 2 c 3 4 8 1 a 2 a 2 a 1 a 3 1 b 3 4 Sample Output 4 5 4 5 11 题意:多组输入,开始字符 ...

随机推荐

  1. python-数据结构Data Structure1

    四种数据结构: 列表list = [val1,val2,val3,val4]字典dict = {key1:val1,key2:val2}元组tuple = (val2,val2,val3,val4)集 ...

  2. Real-Time Rendering.3rd,Radiance与距离无关 的解释

    P208,说radiance与距离无关: 想了半天才想明白: 如图,设入射方向l对应的单位solid angle为dw, 则 沿l方向射入表面的radiance=dw范围内射入表面单位面积上的能量. ...

  3. applicationCache

    <html manifest="/m.appcache"> window.applicationCache.onupdateready = function (e) { ...

  4. Java并发编程(七):线程安全策略

    Java多线程——不可变对象 Java多线程——线程封闭 java线程不安全类与写法 Java线程安全同步容器 Java里的并发容器与安全共享策略总结

  5. Spring Boot(二):@SpringBootApplication注解理解

    @SpringBootApplication包含三个有用的注解,包括 @SpringBootConfiguration:看源码其实就是@Configuration,表示当前类是一个配置类,就像xml配 ...

  6. 怎样优化UITableView的性能

    在iOS App中,UITableView应该是使用率最高的.同一时候也是最为复杂的视图. 差点儿全部自带的应用中都能看到它的身影,可见它的重要性. 在使用UITableView时,会常常遇到性能上的 ...

  7. 将HG版本库推送到Git服务器

    如何将HG版本库推送到Git服务器? 目的 习惯使用HG来进行版本管理,但是GitHub代码统计比Bitbucket要丰富,所以准备主力仓库选用Bitbucket,GitHub作为备用仓库. GitH ...

  8. Atitit.antlr实现词法分析

    Atitit.antlr实现词法分析 1.1.  antlrworks-1.4.3.jar   wizard1 1.2. 词法的类型 id,int,float ,comment,str,char,wh ...

  9. [css]后台管理系统布局

    知识点: 绝对定位+overflowhidden 整体思路 三大块 pg-header---需要固定 (height:48px) pg-content menu 右侧菜单-需要固定(width:200 ...

  10. C#.NET中使用BackgroundWorker在模态对话框中显示进度条

    这里是一个示例,其中展示了如何使用Backgroundworker对象在模态对话框中显示后台操作的实时进度条. 首先是主窗体代码: using System; using System.Collect ...