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;
}

最新文章

  1. iOS应用程序的生命周期
  2. Android编程思想双11口诀
  3. OWIN是什么?
  4. SQL Server安全(1/11):SQL Server安全概述
  5. HTML DOM scale() 方法
  6. Bootstrap页面布局20 - BS缩略图
  7. display:inline-block; 到底是个啥玩意?
  8. SecureCRT连接虚拟机中的Linux系统(Ubuntu)
  9. centos6.6 虚拟机集群搭建
  10. SPI、I2C、UART(转)
  11. UVA-10954 贪心+优先队列
  12. 查询SQL执行情况
  13. Java知多少(81)框架窗口基础
  14. Java设计模式(21)访问模式(Visitor者模式)
  15. less语言特性(一) —— 变量
  16. POJ 1947 Rebuilding Road(树形DP)
  17. [二十三]SpringBoot 之 redis
  18. 关于映射异常org.hibernate.MappingException: An association from the table DUTY_INFO refers to an unmapped class: com.pms.entities.other.Department的原因。
  19. 【bzoj4373】算术天才⑨与等差数列
  20. UVA 10200 Prime Time 水

热门文章

  1. 企业信息系统集成框架(设计思路)C模式
  2. java 解析webservice 中的soapheader
  3. Firefly 3288又一次制作android和lubuntu双系统固件
  4. 论Top与ROW_NUMBER读取第一页的效率问题及拼接sql查询条件
  5. Springboot接收参数
  6. mysql-shell的安装和使用
  7. 有关linux磁盘分区优化
  8. 多线程-CAS原理
  9. redis源码学习_字典
  10. IntelliJ IDEA代码编码区提示库源不匹配字节码解决办法