HDU3948 & 回文树模板
Description:
求本质不同回文子串的个数
Solution:
回文树模板,学一学贴一贴啊...
Code:
/*=================================
# Created time: 2016-04-20 20:55
# Filename: hdu3948.cpp
# Description:
=================================*/
#define me AcrossTheSky&HalfSummer11
#include <cstdio>
#include <cmath>
#include <ctime>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> #include <set>
#include <stack>
#include <queue>
#include <vector> #define lowbit(x) (x)&(-x)
#define Abs(x) ((x) > 0 ? (x) : (-(x)))
#define FOR(i,a,b) for((i)=(a);(i)<=(b);(i)++)
#define FORP(i,a,b) for(int i=(a);i<=(b);i++)
#define FORM(i,a,b) for(int i=(a);i>=(b);i--)
#define ls(a,b) (((a)+(b)) << 1)
#define rs(a,b) (((a)+(b)) >> 1)
#define getlc(a) ch[(a)][0]
#define getrc(a) ch[(a)][1] #define maxn 100005
#define maxc 30
#define maxm 100005
#define INF 1070000000
using namespace std;
typedef long long ll;
typedef unsigned long long ull; template<class T> inline
void read(T& num){
num = 0; bool f = true;char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') f = false;ch = getchar();}
while(ch >= '0' && ch <= '9') {num = num * 10 + ch - '0';ch = getchar();}
num = f ? num: -num;
}
int outs[100];
template<class T> inline
void write(T x){
if (x==0) {putchar('0'); putchar('\n'); return;}
if (x<0) {putchar('-'); x=-x;}
int num=0;
while (x){ outs[num++]=(x%10); x=x/10;}
FORM(i,num-1,0) putchar(outs[i]+'0'); putchar('\n');
}
/*==================split line==================*/
int tot[maxn],num[maxn],len[maxn],fail[maxn],S[maxn],ch[maxn][maxc];
char s[maxn];
int p,last,ans,cnt;
int newnode(int l){
tot[p]=0; num[p]=0; len[p]=l;
return p++;
}
void init(){
p=0; memset(ch,0,sizeof(ch));
newnode(0); newnode(-1);
last=0; cnt=0; S[cnt]=-1; fail[0]=1;
}
int get_fail(int x){
while(S[cnt-len[x]-1]!=S[cnt]) x=fail[x];
return x;
}
void add(int c,int pos){
S[++cnt]=c;
int cur=get_fail(last);
if (!ch[cur][c]){
int now=newnode(len[cur]+2);
fail[now]=ch[get_fail(fail[cur])][c];
ch[cur][c]=now; ans++;
}
last=ch[cur][c];
tot[last]++;
}
int main(){
int cas; read(cas); int Cas=1;
while (Cas<=cas){
printf("Case #%d: ",Cas);
ans=0;
scanf("%s",s);
int n=strlen(s);
init();
FORP(i,0,n-1) add(s[i]-'a'+1,i);
write(ans);
Cas++;
}
}
HDU3948 & 回文树模板的更多相关文章
- 南京网络赛I-Skr【回文树模板】
19.32% 1000ms 256000K A number is skr, if and only if it's unchanged after being reversed. For examp ...
- BZOJ - 3676 回文串 (回文树)
https://vjudge.net/problem/HYSBZ-3676 题意 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出 现值”为t在s中的出现次数乘以t的长度.请你求出s ...
- HDU.5394.Trie in Tina Town(回文树)
题目链接 \(Description\) 给定一棵\(Trie\).求\(Trie\)上所有回文串 长度乘以出现次数 的和.这里的回文串只能是从上到下的一条链. 节点数\(n\leq 2\times ...
- BZOJ.3676.[APIO2014]回文串(回文树)
BZOJ 洛谷 很久之前写(抄)过一个Hash+Manacher的做法,当时十分懵逼=-= 然而是道回文树模板题. 回文树教程可以看这里(真的挺妙的). 顺便再放上MilkyWay的笔记~ //351 ...
- HYSBZ 2160 拉拉队排练(回文树)
2160: 拉拉队排练 Time Limit: 10 Sec Memory Limit: 259 MB Submit: 825 Solved: 324 [Submit][Status][Discu ...
- HYSBZ 3676 回文串 (回文树)
3676: [Apio2014]回文串 Time Limit: 20 Sec Memory Limit: 128 MB Submit: 1680 Solved: 707 [Submit][Stat ...
- BZOJ 3676 回文串(回文树)题解
题意: 一个回文的价值为长度 * 出现次数,问一个串中的子串的最大回文价值 思路: 回文树模板题,跑PAM,然后计算所有节点出现次数. 参考: 回文串问题的克星--Palindrome Tree(回文 ...
- [模板] 回文树/回文自动机 && BZOJ3676:[Apio2014]回文串
回文树/回文自动机 放链接: 回文树或者回文自动机,及相关例题 - F.W.Nietzsche - 博客园 状态数的线性证明 并没有看懂上面的证明,所以自己脑补了一个... 引理: 每一个回文串都是字 ...
- 2014-2015 ACM-ICPC, Asia Xian Regional Contest G The Problem to Slow Down You 回文树
The Problem to Slow Down You Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjud ...
随机推荐
- java基础 布局管理器
概念: 组建在容器(比如JFrame)中的位置和 大小 是由布局管理器来决定的.所有的容器都会使用一个布局管理器,通过它来自动进行组建的布局管理. 种类: java共提供了物种布局管理器:流式布局管理 ...
- 2. 上传Android代码到github
1. 建立git仓库 cd到本地项目根目录,执行git命令 git init 同时会在项目根目录下生成一个.git的隐藏文件 windows下先禁用 ...
- U3D学习笔记1: HelloWorld
Unity 版本: 5.3.5.f1 Hello World工程 1.新建工程 HelloWorld U3D可选2D和3D游戏 2.新建C#脚本文件 在project栏的assets目录右键-&g ...
- Unity 编译错误记录
1. 相关代码: NetworkView.RPC ("ReceiveMessage", RPCMode.All, message); 编译输出: Assets/cs/ClientC ...
- 基于log4net的帮助类Log
using log4net; using System; using System.Collections.Generic; using System.Diagnostics; using Syste ...
- mysql character set exception
问题: 插入数据时,报了这样一个错误:“_mysql_exceptions.Warning: Incorrect string value: ‘\xE6\xB5\x81\xE8\xA1\x8C…’ f ...
- JSP复习整理(五)JavaBean使用表单处理数据
一.先建立用户输入的数据 usingGetparameter.html <!DOCTYPE html> <html> <head> <meta charset ...
- css初始化代码
最近老有新项目开发,一直在找存留的CSS初始化代码,索性放到这里备份下, @charset "utf-8"; /* -------------------------------- ...
- RBAC基于角色的访问控制
RBAC(Role-Based Access Control,基于角色的访问控制),就是用户通过角色与权限进行关联.简单地说,一个用户拥有若干角色,每一个角色拥有若干权限.这样,就构造成"用 ...
- gloolooer
V1.2gloolooer金[http://sh.yun.ftn.qq.com/ftn_handler/0a9043ee7aa120d3c864281211dff2f08efe5e2d42438ec5 ...