题意:给你一个字符串,给你Q次询问,每一次问你从l-r里有多少个回文串。

  思路:len很小,所以直接遍历区间求就好了。

/*  gyt
Live up to every day */
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<cstring>
#include<queue>
#include<set>
#include<string>
#include<map>
#include <time.h>
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = +;
const int sigma=;
const ll mod = ;
const int INF = 0x3f3f3f;
const db eps = 1e-;
struct ptree{
char s[maxn];
int next[maxn][sigma], fail[maxn], cnt[maxn], len[maxn];
int last, n, p;
ll res;
inline int newnode(int l) {
cnt[p]=;
len[p]=l;
return p++;
}
inline void init() {
n=, p=, last=;
memset(next, , sizeof(next));
memset(cnt, , sizeof(cnt));
memset(len, , sizeof(len));
memset(fail, , sizeof(fail));
newnode(), newnode(-);
s[n]=-;
fail[]=;
//cout<<n<<" "<<p<<" "<<last<<endl;
}
inline int FL(int x) {
while(s[n-len[x]-]!=s[n]) x=fail[x];
return x;
}
void add(char c) {
c-='a';
s[++n]=c;
int cur=FL(last);
if (!next[cur][c]) {
int now=newnode(len[cur]+);
fail[now]=next[FL(fail[cur])][c];
next[cur][c]=now;
}
last=next[cur][c];
++cnt[last];
}
inline ll countt() {
ll pk=;
for (int i=p-; ~i; --i) {
cnt[fail[i]]+=cnt[i];
pk++;
}
return pk;
}
}p;
char s[maxn];
int ans[maxn][maxn];
void solve(){
memset(ans, , sizeof(ans));
scanf("%s",s);
int len=strlen(s);
for (int i=; i<len; i++) {
p.init();
for (int j=i; j<len; j++) {
p.add(s[j]);
ans[i][j] = p.p-;
}
}
int q; scanf("%d", &q);
while(q--) {
int a, b; scanf("%d%d", &a, &b);
printf("%d\n", ans[a-][b-]);
}
}
int main() {
int t = ;
// freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
scanf("%d", &t);
while(t--)
solve();
return ;
}

HDU - 5658的更多相关文章

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

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

  2. 回文树练习 Part1

    URAL - 1960   Palindromes and Super Abilities 回文树水题,每次插入时统计数量即可. #include<bits/stdc++.h> using ...

  3. Hdu 5213-Lucky 莫队,容斥原理,分块

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5213 Lucky Time Limit: 6000/3000 MS (Java/Others)    Me ...

  4. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  6. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  7. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  8. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  9. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

随机推荐

  1. 第二章 向量(d4)有序向量:二分查找(改进)

  2. NAT和Proxy的区别

    在internet共享上网技术上,一般有两种方式,一种是proxy代理型,一种是NAT网关型,关于两者的区别与原理,身边很多人都不是很明白,下面我来讲讲我的理解,如有不对的,欢迎指正. 1.先说应用例 ...

  3. 算法篇【递归2 -- N皇后问题】

    问题:输入整数N,要求在N*N的棋盘上,互相不能攻击,不在同一行同一列上,切不在对角线上,输出全部方案. 输入: 4 输出: 2  4  1  3 3  1  4  2 思路: 假设在前k-1个摆好的 ...

  4. NIO和IO(BIO)的区别及NIO编程介绍

    IO(BIO)和NIO的区别:其本质就是阻塞和非阻塞的区别. 阻塞概念:应用程序在获取网络数据的时候,如果网络传输数据很慢,那么程序就一直等着,直到传输完毕为止. 非阻塞概念:应用程序直接可以获取已经 ...

  5. 并发编程中Future和Callable使用

    Future模式非常适合在处理很耗时很长的业务逻辑时进行使用,可以有效的减少系统的响应时间,提高系统的吞吐量. 看一个小的demo: 看一下执行结果: 这是异步去获取结果的示例,在子线程去处理任务的时 ...

  6. React学习札记一

    I’m in a hurry! 我在赶时间! It’s her field. 这是她的本行. It’s up to you. 由你决定. You owe me one.你欠我一个人情. 1.React ...

  7. [Java学习]面向对象-super关键字;final关键字

    super关键字 super代表的是当前子类对象中的父类型特征,可以看做是this的一部分.与this不同,不是引用,不存储对象内存地址. super可以用在什么位置 1 可以用在成员方法中.不能用在 ...

  8. 面向对象中的property装饰器讲解

    面向对象中可以用property来修饰我们的函数,必须下面的例子 class Test(object): def __init__(self,name): self.name = name @prop ...

  9. Django的model操作中一些常用的小点

    一.django的orm的13种基本的查询方法 1.all方法,查询某张表中的所有的数据 user_obj_list = models.Person.objects.all() 返回的结果是query ...

  10. java_14.1 判断是否是闰年

    package demo; import java.util.Calendar; import java.util.Scanner; public class Demo { public static ...