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.

InputFirst 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
OutputFor each testcase, output the answer in Q lines.Sample Input

1
abba
2
1 2
1 3

Sample Output

2
3

题意:给定字符串S,|S|<1000;Q次询问区间不用本质的回文串数量。

思路:以每个左端点建立回文树即可。 注意判定边界,即给第0位赋值-1啥的,我之前的板子没有,害我wa了几发。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
char c[maxn]; int bb[maxn];
int N,Q,fcy[maxn][maxn];
struct PT
{
struct node{
int fail,len,son[];
}t[maxn];
int tot,last;
void init()
{
memset(t,,sizeof(t));
t[].fail=t[].fail=;
t[].len=-;
last=; tot=; bb[]=-; bb[]=-;
}
void add(int s,int n)
{
int p=last; bb[n]=s;
while(bb[n-t[p].len-]!=bb[n]) p=t[p].fail;
if(!t[p].son[s])
{
int v=++tot,k=t[p].fail;
t[v].len=t[p].len+;
while(bb[n-t[k].len-]!=bb[n]) k=t[k].fail;
t[v].fail=t[k].son[s];
t[p].son[s]=v;
}
last=t[p].son[s];
}
}T;
void solve()
{
rep(i,,N){
T.init();
rep(j,i,N) {
T.add(c[j]-'a',j-i+);
fcy[i][j]=T.tot-;
}
}
scanf("%d",&Q);
rep(i,,Q){
int L,R; scanf("%d%d",&L,&R);
printf("%d\n",fcy[L][R]);
}
}
int main()
{
int T;scanf("%d",&T);
while(T--){
memset(c,,sizeof(c));
scanf("%s",c+);
N=strlen(c+);
solve();
}
return ;
}

HDU5658:CA Loves Palindromic (回文树,求区间本质不同的回文串数)的更多相关文章

  1. HDU - 5157 :Harry and magic string (回文树,求多少对不相交的回文串)

    Sample Input aca aaaa Sample Output 3 15 题意: 多组输入,每次给定字符串S(|S|<1e5),求多少对不相交的回文串. 思路:可以用回文树求出以每个位置 ...

  2. HDU 5658 CA Loves Palindromic(回文树)

    CA Loves Palindromic Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/O ...

  3. 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)

    原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...

  4. 主席树——求区间[l,r]不同数字个数的模板(向左密集 D-query)

    主席树的另一种用途,,(还有一种是求区间第k大,区间<=k的个数) 事实上:每个版本的主席树维护了每个值最后出现的位置 这种主席树不是以权值线段树为基础,而是以普通的线段树为下标的 /* 无修改 ...

  5. xdoj-1324 (区间离散化-线段树求区间最值)

    思想 : 1 优化:题意是覆盖点,将区间看成 (l,r)转化为( l-1,r) 覆盖区间 2 核心:dp[i]  覆盖从1到i区间的最小花费 dp[a[i].r]=min (dp[k])+a[i]s; ...

  6. hdu 1754 I Hate It (线段树求区间最值)

    HDU1754 I Hate It Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u D ...

  7. [csu/coj 1080]划分树求区间前k大数和

    题意:从某个区间内最多选择k个数,使得和最大 思路:首先题目给定的数有负数,如果区间前k大出现负数,那么负数不选和更大,于是对于所有最优选择,负数不会出现,所以用0取代负数,问题便转化为区间的前k大数 ...

  8. HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  9. SPOJ DQUERY (主席树求区间不同数个数)

    题意:找n个数中无修改的区间不同数个数 题解:使用主席树在线做,我们不能使用权值线段树建主席树 我们需要这么想:从左向右添加一到主席树上,添加的是该数字处在的位置 但是如果该数字前面出现过,就在此版本 ...

随机推荐

  1. SourceInsight 4重启之后文件变只读无法编辑

    SourceInsight4.0在导入代码后,用起来没问题,第二天,再开启sourceInsight,结果所有文件变成只读了,不能编辑,标签前面也有了叹号. 百度一下,有人说是版本控制的问题,但是sv ...

  2. UNIX发展史简介

    1965年贝尔实验室(Bell Labs).通用电气(General Electric)和麻省理工学院(MIT)欲共同打造MULTICS(Multiplexed Information and Com ...

  3. hive top n

    hive 中窗口函数row_number,rank,dense_ran,ntile分析函数的用法 hive中一般取top n时,row_number(),rank,dense_ran()这三个函数就派 ...

  4. Spring Cloud之路:(七)SpringBoot+Shiro实现登录认证和权限管理

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/sage_wang/article/details/79592269一.Shiro介绍1.Shiro是 ...

  5. spark使用正则表达式读入多个文件

    String dir = "s3a://example/";String currentDir = dir + "{1[5-9],2[01]}/*.txt";J ...

  6. 人机交互之QQ拼音

    随着电脑系统的不断更新,电脑自带的系统输入法也越来越适应广大群众了,那为什么其他的输入法还能占有大量的用户呢? 首先系统输入法是面向大众的,个性化方面略显不足. 其次系统输入法功能不够全面,比如说手写 ...

  7. Unity运行错误代码处理

    1.Unity在运行时出现如图错误,但不影响运行效果展示. 2.错误原因:代码不规范. 3.检查代码,查看变量是否定义正确.

  8. eclipse package视图和navigator视图的区别

    package视图是适合开发的视图,因为开发时我们只关注源文件,并不关注编译后的二进制文,所有在该视图中存放二进制文件的classes文件被隐藏了,而navigator视图,就是项目在工作空间中存放的 ...

  9. zTree入门实例(一眼就看会)

    zTree 是一个依靠 jQuery 实现的多功能 “树插件”. 下载地址:https://gitee.com/zTree/zTree_v3 待会将上面划的三个文件复制到Java的Web工程下即可 先 ...

  10. hdu-2063-过山车(匈牙利算法)

    过山车 Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找 ...