HDU - 5658
题意:给你一个字符串,给你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的更多相关文章
- HDU 5658 CA Loves Palindromic(回文树)
CA Loves Palindromic Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/O ...
- 回文树练习 Part1
URAL - 1960 Palindromes and Super Abilities 回文树水题,每次插入时统计数量即可. #include<bits/stdc++.h> using ...
- Hdu 5213-Lucky 莫队,容斥原理,分块
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5213 Lucky Time Limit: 6000/3000 MS (Java/Others) Me ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
随机推荐
- 如何向 Windows 7 镜像中添加 USB3.0 驱动
如何向 Windows 7 镜像中添加 USB3.0 驱动 1. Microsoft 在 Windows 7 的安装光盘并没有集成各个厂商的 USB3.0 驱动,可 以使用下面方法添加 USB3.0 ...
- JS 解决json字符串转换成json树形输出
问题: 后台获取一个字符串,格式为 string +jsonList+string+..... 就是传过来一堆数据,然后其中包含了一个json格式的list, 我们希望能将它输出成树形结构显示,能够 ...
- Appium+python自动化3-定位元素
3.1常用定位方法讲解 对象定位是自动化测试中很关键的一步,也可以说是最关键的一步,毕竟你对象都没定位那么你想操作也不行.所以本章节的知识我希望大家多动手去操作,不要仅仅只是书本上的知识,毕竟这个我只 ...
- 修改django后台用户名和密码
cd到manage.py目录下 python manage.py shell >>from django.contrib.auth.models import User >>u ...
- POJ 1684 Corn Fields(状压dp)
描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ ...
- [剑指Offer]42-连续子数组的最大和(DP)
题目链接 https://www.nowcoder.com/practice/459bd355da1549fa8a49e350bf3df484?tpId=13&tqId=11183&t ...
- 利用sshtunnel实现跳板机的效果[嵌套ssh实现]
with SSHTunnelForwarder ( ssh_address_or_host = (conf.server_ip,conf.server_port), ssh_username=conf ...
- linux修改hosts
vim /etc/hosts
- centos7 二进制版本安装 mysql8.0.13
一.下载mysql二进制安装包 mysql官网: https://dev.mysql.com/downloads/mysql/ 由于国内网络问题,个人建议使用国内开源镜像站下载: http://mir ...
- cookie session ORM 操作
. ORM增删改查操作 http://www.cnblogs.com/liwenzhou/p/8660826.html . 单表增删改查 . 单表的双下划线操作 . 外键的跨表查询 . 正向查询 . ...