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. JavaScript点击按钮显示 确认对话框

    //JavaScript点击按钮显示确认对话框 <html xmlns="http://www.w3.org/1999/xhtml"> <head> < ...

  2. oracle 复杂的查找用法

    [第一题]: 找到员工表中工资最高的前三名,要求按如下格式输出(第一步部分):以及oracle查询结果指定分页显示的方法(第二部分). ——涉及Top-N分析问题. 一般不在子查询中使用order b ...

  3. Firefly 3288又一次制作android和lubuntu双系统固件

    又一次制作android和lubuntu双系统固件 因为本人改动了lubuntu的驱动和设备树信息,为了方便烧写系统,所以又一次制作了双系统的固件. Firefly wiki教程里有android固件 ...

  4. FreeSWITCH技巧:如何向通话的另一方号码发送dtmf?

    注:这里的文章都是本人的日常总结,请尊重下个人的劳动成果,转载的童鞋请注明出处,谢谢. 如您转载的文章发生格式错乱等问题而影响阅读,可与本人联系,无偿提供本文的markdown源代码. 联系邮箱:ji ...

  5. 捅伊朗黑客PP — 后台登陆POST+错误回显 注入

    看了一个泰国政府的网站被伊朗的黑客挂页,上面写着“Your Box 0wn3z By Behrooz_Ice – Q7x -Sha2ow -Virangar -Ali_Eagle -iman_takt ...

  6. 关于Animator获取当前剪辑长度

    通常下意识的肯定用这个接口 GetCurrentAnimatorStateInfo().length 但是存在一个过渡动画的问题,具体看这篇:过渡动画的测试 所以当播新的状态时直接取动画时间,取到的就 ...

  7. ado连接sql server

    //ado连接sql server //头文件加上以下这句. #import "C:\Windows\system\msado15.dll" no_namespace rename ...

  8. 李洪强和你一起学习前端之(5)css书写位置 优先级和伪类

    亲爱的,时间过得真快,不知不觉我们一起学习前端已经4天了,这4天的时间里,我们是不是收获很大呢,每当我们学习一个新的知识点的时候,每当我们做出一个新的东西来的时候,我们是不是欣喜若狂,世界从来否不会辜 ...

  9. Android App常规测试内容

    转自:https://mp.weixin.qq.com/s?__biz=MzU0NjcyNDg3Mw==&mid=2247484053&idx=1&sn=116fe8c7eed ...

  10. Django Model之引用一个未定义的Model

    Django Model的外键自关联