HDU 5658 CA Loves Palindromic(回文树)
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
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.
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
1
abba
2
1 2
1 3
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(回文树)的更多相关文章
- HDU - 5785:Interesting (回文树,求相邻双回文的乘积)
Alice get a string S. She thinks palindrome string is interesting. Now she wanna know how many three ...
- 回文树练习 Part1
URAL - 1960 Palindromes and Super Abilities 回文树水题,每次插入时统计数量即可. #include<bits/stdc++.h> using ...
- HDU5658:CA Loves Palindromic (回文树,求区间本质不同的回文串数)
CA loves strings, especially loves the palindrome strings. One day he gets a string, he wants to kno ...
- 回文树 Palindromic Tree
回文树 Palindromic Tree 嗯..回文树是个什么东西呢. 回文树(或者说是回文自动机)每个节点代表一个本质不同的回文串. 首先它类似字典树,每个节点有SIGMA个儿子,表示对应的字母. ...
- HDU 5421 Victor and String(回文树)
Victor and String Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/262144 K (Java/Othe ...
- HDU.5394.Trie in Tina Town(回文树)
题目链接 \(Description\) 给定一棵\(Trie\).求\(Trie\)上所有回文串 长度乘以出现次数 的和.这里的回文串只能是从上到下的一条链. 节点数\(n\leq 2\times ...
- Palindromic Tree 回文自动机-回文树 例题+讲解
回文树,也叫回文自动机,是2014年被西伯利亚民族发明的,其功能如下: 1.求前缀字符串中的本质不同的回文串种类 2.求每个本质不同回文串的个数 3.以下标i为结尾的回文串个数/种类 4.每个本质不同 ...
- ZOJ 3661 Palindromic Substring(回文树)
Palindromic Substring Time Limit: 10 Seconds Memory Limit: 65536 KB In the kingdom of string, p ...
- 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 题意:多组输入,开始字符 ...
随机推荐
- 在Ubuntu下安装mongodb
一. 在Ubuntu下最傻瓜的步骤(以下都在root用户下进行操作): 1.运行"apt-get install mongo" 如果遇到找不到安装包的话运行"apt-ge ...
- Struts2架构分析和执行机制
实例分析 1.在浏览器中输入url地址后,会通过http协议发送给tomcat,tomacat收到请求后查看訪问的是哪个 webapplication(例如以下图的Struts2_0100_Intro ...
- Wpf ScrollBar自定义样式
Wpf的ScrollBar可以分为六个区域:A.背景.B.向上按钮.C.向下的按钮.D.Track里面向上的按钮.E.Track里面向下的按钮.F.Track的Thumb 详情见下图 下面通过一个例子 ...
- Java并发编程(四):线程安全性
一.定义 当多个线程访问某个类时,不管运行时环境采用何种调度方式或者这些进程将如何交替执行,并且在主调代码中不需要额外的同步或协同,这个类都能表现出正确的行为,那么就称这个类是线程安全的. 二.线程安 ...
- poj 2846 Repository
题目大意:给你n个字符串,然后给你m个子串,看这个子串在上面的多少个串中,出现过: 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2846 本题可以在字 ...
- 0044 spring框架的applicationContext.xml的命名空间
Spring框架中,创建bean,装配bean,事务控制等,可以用xml配置或者注解扫描的方法实现.如果用注解扫描,在xml配置中得加上 <context:component-scan base ...
- ecmall的物流配送体系改造
接触多了ecshop.ecmall原始逻辑的,一般都习惯以整单的方式统一计算运费,这是一种很简单的思路. 但淘宝多了,就发现,物流运费没有那么简单. 首先,每种商品单独设置运费的体系,或者叫运费模板: ...
- SVN 提交出错:Attempted to lock an already-locked dir
http://www.2cto.com/kf/201306/221414.html —————————————————————————————————————————————————————— 在ec ...
- redis php 实例二
前面一篇博客主要是string类型,list类型和set类型,下面hash类型和zset类型 1,hset 描述:将哈希表key中的域field的值设为value.如果key不存在,一个新的哈希表被创 ...
- ios 页面过长卡顿的情况
解决方案如下: -webkit-overflow-scrolling: touch;