HDU-5157Harry and magic string
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5157
先从后往前插点,在构造回文树时,让cnt[i]+=cnt[fail[i]],然后维护一个后缀和a。
再从前往后插点,每个点对答案的贡献为cnt[i]*a[i+1]
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdio>
#define rep(i,l,r) for (int i=l;i<=r;i++)
#define down(i,l,r) for (int i=l;i>=r;i--)
#define clr(x,y) memset(x,y,sizeof(x))
#define ll long long
#define maxn 200500
using namespace std;
ll a[maxn],ans;
char ch[maxn];
int read(){
int x=,f=; char ch=getchar();
while (!isdigit(ch)) {if (ch=='-') f=-; ch=getchar();}
while (isdigit(ch)) {x=x*+ch-''; ch=getchar();}
return x*f;
}
struct data{
ll sum;
int n,tot,fail[maxn],l[maxn],nxt[maxn][],s[maxn],last,cnt[maxn];
int newnode(int len){
clr(nxt[tot],);
l[tot]=len; cnt[tot]=;
return tot++;
}
void init(){
tot=; sum=; clr(fail,);
newnode(); newnode(-);
fail[]=;
last=;
s[]=-;
n=;
}
int getfail(int v){
while (s[n-l[v]-]!=s[n]) v=fail[v];
return v;
}
int add(int c){
s[++n]=c;
int cur=getfail(last);
if (!nxt[cur][c]){
int now=newnode(l[cur]+);
fail[now]=nxt[getfail(fail[cur])][c];
nxt[cur][c]=now;
cnt[now]=cnt[fail[now]]+;
} last=nxt[cur][c];
return cnt[last];
}
}T;
int main(){
// freopen("in.txt","r",stdin);
while (~scanf("%s",ch)){
clr(a,); ans=;
int len=strlen(ch);
T.init();
down(i,len-,){
a[i]=a[i+]+T.add(ch[i]-'a');
}
// down(i,len-1,0) printf("%lld ",a[i]);
T.init();
rep(i,,len-) {
ans+=a[i+]*T.add(ch[i]-'a');
}
printf("%lld\n",ans);
}
return ;
}
HDU-5157Harry and magic string的更多相关文章
- HDU 5157 Harry and magic string(回文树)
Harry and magic string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- hdu 3183 A Magic Lamp RMQ ST 坐标最小值
hdu 3183 A Magic Lamp RMQ ST 坐标最小值 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 题目大意: 从给定的串中挑 ...
- HDU 5842 Lweb and String(Lweb与字符串)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- hdu 4850 Wow! Such String! 欧拉回路
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4080264.html 题目链接:hdu 4850 Wow! Such String! 欧拉回 ...
- hdu 3183 A Magic Lamp(RMQ)
题目链接:hdu 3183 A Magic Lamp 题目大意:给定一个字符串,然后最多删除K个.使得剩下的组成的数值最小. 解题思路:问题等价与取N-M个数.每次取的时候保证后面能取的个数足够,而且 ...
- hdu 3553 Just a String (后缀数组)
hdu 3553 Just a String (后缀数组) 题意:很简单,问一个字符串的第k大的子串是谁. 解题思路:后缀数组.先预处理一遍,把能算的都算出来.将后缀按sa排序,假如我们知道答案在那个 ...
- hdu5157 Harry and magic string【manacher】
Harry and magic string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- WPF系列之三:实现类型安全的INotifyPropertyChanged接口,可以不用“Magic string” 么?
通常实现INotifyPropertyChanged接口很简单,为你的类只实现一个PropertyChanged 的Event就可以了. 例如实现一个简单的ViewModel1类: public cl ...
- HDU 3183.A Magic Lamp-区间找最小值-RMQ(ST)
A Magic Lamp Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 4421 Bit Magic(2-sat)
HDU 4421 Bit Magic pid=4421" target="_blank" style="">题目链接 题意:就依据题目,给定b数 ...
随机推荐
- Principle-初步认识(简介)
Principle官网 探究了一下 . 呃--作出了下边这玩意 做的好的是这样的,瞬间把自己给菜了,给大家看看,设计需要UI功夫啊 把这个用上你的界面就搞基了,图形在水平.垂直上的动态效果(*.*) ...
- 转:iOS开发之多种Cell高度自适应实现方案的UI流畅度分析
本篇博客的主题是关于UI操作流畅度优化的一篇博客,我们以TableView中填充多个根据内容自适应高度的Cell来作为本篇博客的使用场景.当然Cell高度的自适应网上的解决方案是铺天盖地呢,今天我们的 ...
- Python 初体验
2017的最后一天,在QC的谆谆教诲下,我终于写(背)了九道题,对Python的基本语法有了一个大致了解. 1.A+B+C 就是为了练输入,line=input().split() 录入列表,分割开 ...
- openstack操作之二 restful api
Restful api 是openstack各服务调用的接口,简单理解为可以通过网络去调用的函数.postman是一款前端调用工具,测试后端接口的时候往往是使用该工具去验证.在openstack的使用 ...
- bat获取系统时间修改系统密码
@echo off %1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe&q ...
- rabbitmq 启动报错
=============================================== 2017/10/24_第1次修改 ccb_warlock = ...
- vim编辑器的使用技巧
vim(vi)是上Linux非常常用的编辑器,很多Linux发行版都默认安装了vi(vim).vi(vim)命令繁多但是如果使用灵活之后将会大大提高效率.vi是“visual interface”的缩 ...
- elastaticresearch 学习过程
1.在Windows上安装了es 2.在chrome上装了sense 3.尝试创建 es 的模板
- JavaScript的DOM编程--11--插入节点
插入节点: 1). insertBefore(): 把一个给定节点插入到一个给定元素节点的给定子节点的前面 var reference = element.insertBefore(newNode,t ...
- C# 真正能发邮件的源码
在网上找了很多例子都试邮件发送都失败,今天无意有试了一下居然行了 public static void ErrorMessageMail(string _subject, string _body) ...