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数 ...
随机推荐
- 2018第一发:记一次【Advanced Installer】打包之旅
一.前言 2017年最后几天,你们都高高兴兴的跨年,博主还在加班制作.net安装包.因为年前要出来第一版的安装包,所以博主是加班加点啊.本来想用VS自带的制作工具,不过用过的人都知道,真是非常好(to ...
- Sum of AP series——AP系列之和
A series with same common difference is known as arithmetic series. The first term of series is 'a' ...
- open-falcon(v0.2)安装grafana部署
下载rpm wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-4.4.3-1.x86_64.rpm 本地 ...
- 前端MVC Vue2学习总结(五)——表单输入绑定、组件
一.表单输入绑定 1.1.基础用法 你可以用 v-model 指令在表单控件元素上创建双向数据绑定.它会根据控件类型自动选取正确的方法来更新元素.尽管有些神奇,但 v-model 本质上不过是语法糖, ...
- Java 包装类Integer的值比较
对于包装类型Integer的值比较与int的值比较是不同的: public class Java_Val_Compare { public static void main(String[] ar ...
- 浅谈OGNL表达式
OGNL(Object-Graph Navigation Language):对象视图导航语言 ${user.addr.name}这样的写法就叫对象视图导航 OGNL不仅可以视图导航,支持EL表达式更 ...
- ${param.name}和${name}的区别
${param.name} == request.getParam("name") ${name} == request.getAttribute("name" ...
- python中的if __name__=='__main__': main()解析
python中我们会看到一段代码是这样的: if __name__=='__main__': main() 这段代码的什么意思,我们可以知道代码的意思是如果__name__=='__main__'为T ...
- 通过!important设置css样式优先级
CSS写在不同的地方有不同的优先级,一般 .css文件中的定义 < 元素style中的属性,但是如果使用!important,则会变得不一样,使用!important的css定义是拥有最高的优先 ...
- VBox 一款基于vue开发的音乐盒 序章
己基于vue写了一个 Mplayer, github地址:https://github.com/xiangwenhu/MPlaer, 演示地址:http://babydairy2017.cloudap ...