HDU5658:CA Loves Palindromic (回文树,求区间本质不同的回文串数)
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 (回文树,求区间本质不同的回文串数)的更多相关文章
- HDU - 5157 :Harry and magic string (回文树,求多少对不相交的回文串)
Sample Input aca aaaa Sample Output 3 15 题意: 多组输入,每次给定字符串S(|S|<1e5),求多少对不相交的回文串. 思路:可以用回文树求出以每个位置 ...
- HDU 5658 CA Loves Palindromic(回文树)
CA Loves Palindromic Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/O ...
- 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)
原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...
- 主席树——求区间[l,r]不同数字个数的模板(向左密集 D-query)
主席树的另一种用途,,(还有一种是求区间第k大,区间<=k的个数) 事实上:每个版本的主席树维护了每个值最后出现的位置 这种主席树不是以权值线段树为基础,而是以普通的线段树为下标的 /* 无修改 ...
- xdoj-1324 (区间离散化-线段树求区间最值)
思想 : 1 优化:题意是覆盖点,将区间看成 (l,r)转化为( l-1,r) 覆盖区间 2 核心:dp[i] 覆盖从1到i区间的最小花费 dp[a[i].r]=min (dp[k])+a[i]s; ...
- hdu 1754 I Hate It (线段树求区间最值)
HDU1754 I Hate It Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u D ...
- [csu/coj 1080]划分树求区间前k大数和
题意:从某个区间内最多选择k个数,使得和最大 思路:首先题目给定的数有负数,如果区间前k大出现负数,那么负数不选和更大,于是对于所有最优选择,负数不会出现,所以用0取代负数,问题便转化为区间的前k大数 ...
- HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- SPOJ DQUERY (主席树求区间不同数个数)
题意:找n个数中无修改的区间不同数个数 题解:使用主席树在线做,我们不能使用权值线段树建主席树 我们需要这么想:从左向右添加一到主席树上,添加的是该数字处在的位置 但是如果该数字前面出现过,就在此版本 ...
随机推荐
- Linux软件源书写格式解析及本地yum源制作
1.Debian类系统 配置文件:/etc/apt/sources.list 例子:deb https://mirrors.aliyun.com/kali kali-rolling main cont ...
- CAS5.3-搭建https服务器
在上一篇文章中https://www.cnblogs.com/zhi-leaf/p/10417627.html.我们使用http://127.0.0.1:8080/cas/登录发现页面显示如下警告.该 ...
- php 图片添加水印和二维码
$host = $_SERVER['HTTP_HOST']; $save_code_file = './qrcodes/qrcode.png'; QrCode::format()->backgr ...
- learning at command AT+CGSN
AT command AT+CGSN [Purpose] Learning how to get mobile module international Mobile Equipment ...
- Five Great .NET Framework 4.5 Features (五大特性)
[译].Net 4.5 的五项强大新特性 本文原文:Five Great .NET Framework 4.5 Features译者:冰河魔法师 目录 介绍 特性一:async和await 特性二 ...
- Python Oracle连接与操作封装
一.封装方式一 #encoding:utf-8 import cx_Oracleclass Oracle_Status_Output: def __init__(self,db_name,db_ ...
- Spring框架的四大原则
Spring框架本身有四大原则: 1).使用POJO进行轻量级和最小入侵式开发 2).通过以来注入和基于接口编程实现松耦合 3).通过AOP和默认习惯进行声明式编程 4).使用AOP和模板减少模式化代 ...
- springboot 打包部署
springboot内置有tomcat所以我们测试的时候没有加入自己的容器 那么我们的 springboot 怎么发布呢? 1.打成 jar 2.打成 war 这种方式我就不说了,网上有教程,我觉得j ...
- angular2组件通讯的几种方式
最近刚刚接触angular2,对ng2也是一知半解,如有说得不对的地方欢迎指出,欢迎加q共同探讨学习991085978: 1.通过输入型绑定把数据从父组件传到子组件 HeroChildComponen ...
- Docker(1):初体验之应用挂载到容器
需在安装docker的机器上运行,本文机器环境为Win10,目录可根据实际自行修改. 1.首先创建一个目录:D:\docker\ROOT\WEB-INF 2.在D:\docker\ROOT\WEB-I ...