hdu 3948 The Number of Palindromes
The Number of Palindromes
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
http://acm.hdu.edu.cn/showproblem.php?pid=3948
distinct substring of S which is palindrome.
T(T<=20), which indicates number of test cases.
Each test case consists of
a string S, whose length is less than 100000 and only contains lowercase
letters.
in a single line, where k indicates the case number and starts at 1. Then output
the number of distinct substring of S which is palindrome.
#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define D1 28
#define D2 38
using namespace std;
const int MOD1=;
const int MOD2=;
const int MOD3=1e9+;
const int MOD4=1e9+;
char s[],a[];
int p[],len,l,ans;
int f1[],g1[];
int f2[],g2[];
int front1[MOD1+],front2[MOD2+];
int to1[],to2[];
int nxt1[],nxt2[];
int tot1,tot2;
void manacher()
{
memset(p,,sizeof(p));
int pos=,id=,x;
for(int i=;i<=len;i++)
{
if(i<pos) x=min(p[*id-i],pos-i);
else x=;
while(s[i+x]==s[i-x]) x++;
if(i+x>pos) { pos=i+x; id=i; }
p[i]=x;
}
}
int get_hash1(int l,int r)
{
int a=f1[r];
int b=1ll*f1[l-]*g1[r-l+]%MOD3;
return (b-a+MOD3)%MOD3;
}
int get_hash2(int l,int r)
{
int a=f2[r];
int b=1ll*f2[l-]*g2[r-l+]%MOD4;
return (b-a+MOD4)%MOD4;
}
void add1(int u,int v)
{
to1[++tot1]=v; nxt1[tot1]=front1[u]; front1[u]=tot1;
}
void add2(int u,int v)
{
to2[++tot2]=v; nxt2[tot2]=front2[u]; front2[u]=tot2;
}
bool find1(int u,int v)
{
for(int i=front1[u];i;i=nxt1[i])
if(to1[i]==v) return true;
return false;
}
bool find2(int u,int v)
{
for(int i=front2[u];i;i=nxt2[i])
if(to2[i]==v) return true;
return false;
}
void cal(int pos,int len)
{
for(int i=len;i>=;i--)
{
int hash1=get_hash1(pos-i+,pos+i-);
int hash2=get_hash2(pos-i+,pos+i-);
int t1=hash1%MOD1,t2=hash2%MOD2;
if(!(find1(t1,hash1)&&find2(t2,hash2)))
{
ans++;
add1(t1,hash1);
add2(t2,hash2);
}
else return;
}
}
void pre()
{
ans=tot1=tot2=;
memset(front1,,sizeof(front1));
memset(front2,,sizeof(front2));
memset(f1,,sizeof(f1));
memset(f2,,sizeof(f2));
g1[]=;
for(int i=;i<=len;i++) g1[i]=1ll*g1[i-]*D1%MOD3;
f1[]=s[];
for(int i=;i<=len;i++) f1[i]=(1LL*f1[i-]*D1+s[i]-'')%MOD3;
g2[]=;
for(int i=;i<=len;i++) g2[i]=1ll*g2[i-]*D2%MOD4;
f2[]=s[];
for(int i=;i<=len;i++) f2[i]=(1LL*f2[i-]*D2+s[i]-'')%MOD4;
}
int main()
{
int t;
scanf("%d",&t);
for(int tt=;tt<=t;tt++)
{
scanf("%s",a);
s[len=]='!';
l=strlen(a);
for(int i=;i<l;i++)
{
s[++len]='#';
s[++len]=a[i];
}
s[++len]='#';
s[len+]='&';
pre();
manacher();
for(int i=;i<=len;i++)
cal(i,p[i]);
printf("Case #%d: %d\n",tt,ans/);
}
}
hdu 3948 The Number of Palindromes的更多相关文章
- HDU 3948 The Number of Palindromes(Manacher+后缀数组)
题意 求一个字符串中本质不同的回文子串的个数. $ 1\leq |string| \leq 100000$ 思路 好像是回文自动机的裸题,但是可以用 \(\text{Manacher}\) (马拉车) ...
- 【HDOJ】3948 The Number of Palindromes
后缀数组求不重复回文子串数目.注意dp数组. /* 3948 */ #include <iostream> #include <sstream> #include <st ...
- hdu 3948 后缀数组
The Number of Palindromes Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (J ...
- hdu 5898 odd-even number 数位DP
传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...
- dp --- Codeforces 245H :Queries for Number of Palindromes
Queries for Number of Palindromes Problem's Link: http://codeforces.com/problemset/problem/245/H M ...
- hdu 2665 Kth number
划分树 /* HDU 2665 Kth number 划分树 */ #include<stdio.h> #include<iostream> #include<strin ...
- 【HDU3948】 The Number of Palindromes (后缀数组+RMQ)
The Number of Palindromes Problem Description Now, you are given a string S. We want to know how man ...
- 【CF245H】Queries for Number of Palindromes(回文树)
[CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...
- 【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)
[SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...
随机推荐
- 阅读 用P4对数据平面进行编程
引言 关于题目,对数据平面进行编程,在之前读过the road to SDN,软件定义网络的思想在于数控分离,其对网络行为的编程暂时只局限于网络控制平面.其转发平面在很大程度上受制于功能固定的包处理硬 ...
- MVC4 DropDownList (一) — 使用方法
1.下面代码包含了三种绑定DropDownList的方法 using System; using System.Collections.Generic; using System.Linq; usin ...
- 多线程Worker初尝试
多线程这个概念,不知道听了多少遍.但是真滴没有去实操过. 前几天看视频听到作者说道关注技术本身,而不是总写业务代码.这几天依然思考着这个问题.于是从头开始重现了html文件的堵塞问题,重现了html文 ...
- CentOS/Linux 开放80、8080端口或者开放某个端口
装载系统的时候只开启了22端口.结果再装完Nginx+php+mysql 后不能访问网站. iptables -L -n 查看防火墙设置发现没开启80端口 由于Linux防火墙默认是关闭的.可以用两种 ...
- 【APS.NET Core】- Razor Page 使用jqgrid实现分页功能
本文将使用jqgrid在Razor Page中实现分页功能. 前台 List.cshtml代码如下: @page @model ListModel @{ Layout = "~/Pages/ ...
- 修改IP的批处理
昨天遇到一个客户,说是抢火车票来着,用了3个公网IP,要求在抢票前15分钟换次IP(看我这毛病,废话多了,正题) 系统是2003 32位的 因为自己不懂脚本,网上找了个修改了下,就有了下面的脚本: 首 ...
- 初学LINUX版本的选择
1.用于企业环境:建议使用商业版本,例如Red Hat的RHEL或者是Novell的SuSE都是很不错的选择!毕竟企业的环境强调的是永续的经营,你可不希望网管人员走了之后整个机房的主机都没有人管理吧! ...
- 第71天:jQuery基本选择器(二)
jQuery选择器 一.内容过滤选择器 选择器 描 述 返 回 示 例 :contains(text) 匹配含有文本内容text的元素 集合元素 $(“p:contains(今天)”) :empty ...
- 【Python】Python中的列表操作
Python的列表操作可谓是功能强大且方便(相对于Java)简单.常规的操作就不说了(这不是一个入门教程),介绍几个很有特点的例子 添加 # 追加到结尾(append) li = [1, 2, 3, ...
- SpringBoot JDBC/AOP
JDBC 工程结构: pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmln ...