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个数中无修改的区间不同数个数 题解:使用主席树在线做,我们不能使用权值线段树建主席树 我们需要这么想:从左向右添加一到主席树上,添加的是该数字处在的位置 但是如果该数字前面出现过,就在此版本 ...
随机推荐
- vue-router 路由钩子(hook)
钩子(hook)—劫持机制 路由钩子(守卫钩子): 1.全局钩子2.某个路由独享的钩子3.组件内钩子 三种路由钩子中都涉及到了三个参数,官方(https://router.vuejs.org/zh-c ...
- MySQL修改root密码教程
1.记得密码但想要更新密码 mysql -uroot -p #使用当前密码登录mysql update MySQL.user set password=PASSWORD('新密码') where Us ...
- 微信小程序 带参调用后台接口 循环渲染页面 wx.request wx:for
test.js 文件里的onLoad function getarticles(p,order,mythis) { wx.request({ url: 'https://ganggouo.cn/ind ...
- php 异常捕获的坑
thinkphp 框架需要注意 书写为(Exception $e)将无效 需要写成 (\Exception $e) try { throw new \Exception("Error P ...
- Java Date实现加一天,年月日类推往后+1,日期+1,月份+1,年份+1
System.out.println("String类型 "+endDate); //页面传递到后台的时间 为String类型 SimpleDateFormat sdf = new ...
- linux下/proc/diskstats文件详解
每一列的含义分别为: 第一列为 设备号 (number of issued reads. This is the total number of reads completed successfull ...
- zabbix3.4.7触发器表达式详解
zabbix触发器表达式详解 概述:触发器中的表达式使用很灵活,我们可以创建一个复杂的逻辑测试监控,触发器表达式形式如下: {<server>:<key>.<functi ...
- SignalR 前期简单配置
一.随便你在哪个命名空间下新建一个Startup类,并在在该类中注册SignalR. using Owin; using System; using System.Collections.Generi ...
- 用FPGA对ASIC进行原型验证的过程(转)
用FPGA对ASIC进行原型验证的过程 reference:http://xilinx.eetrend.com/d6-xilinx/article/2018-10/13736.html 鉴 ...
- YLZ开发外网前端
YLZ外网前端使用的是boottstrap和layer和validform等插件 表格是用boottstrap的tablle加上框架人员开发的ajaxpageresponse进行协调开发 弹窗使用的l ...