BZOJ 3790 神奇项链 hash/后缀自动机+贪心
Description
Input
Output
Sample Input
abacada
abcdef
Sample Output
2
5
HINT
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<cctype>
using namespace std;
const int maxn=;
typedef long long LL;
const int mo=; char S[maxn],T[maxn];
int n,hash1[maxn],hash2[maxn],pw[maxn];
struct data{ int l,r; }line[maxn]; bool check(int i,int mid)
{
int v1=i-mid==?hash1[i]:(hash1[i]-1ll*hash1[i-mid-]*pw[mid+]%mo+mo)%mo;
int v2=i+mid==n-?hash2[i]:(hash2[i]-1ll*hash2[i+mid+]*pw[mid+]%mo+mo)%mo;
return v1==v2;
}
bool cmp(data x,data y){ return x.l<y.l||x.l==y.l&&x.r<y.r; }
void work()
{
n=strlen(T);
hash1[]=T[]-'A'+;
for(int i=;i<n;i++) hash1[i]=(hash1[i-]*89ll+T[i]-'A'+)%mo;
hash2[n-]=T[n-]-'A'+;
for(int i=n-;i>=;i--) hash2[i]=(hash2[i+]*89ll+T[i]-'A'+)%mo;
for(int i=;i<n;i++){
int L=,R=min(i,n-i-)+,mid,len=;
while(L<R){
mid=L+R>>;
if(check(i,mid)) len=mid,L=mid+;
else R=mid;
}
line[i]=(data){i-len,i+len};
}
sort(line,line+n,cmp);
int ans=,pos=,mx=;
for(int i=;i<n;i++){
if(line[i].l>pos) pos=mx,mx=,ans++;
if(line[i].r>mx) mx=line[i].r;
}
printf("%d\n",max(,ans-));
}
int main()
{
pw[]=;
for(int i=;i<=;i++) pw[i]=pw[i-]*89ll%mo;
while(scanf("%s",S)==){
int cnt=; n=strlen(S);
T[cnt++]='A';
for(int i=;i<n;i++)
T[cnt++]=S[i],T[cnt++]='A';
T[cnt]='\0';
work();
}
return ;
}
回文自动机:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<cctype>
#include<ctime>
using namespace std;
const int maxl=; char S[maxl];
int r[maxl];
struct PAM{
static const int maxn=;
int sz,last,to[maxn][],len[maxn],fail[maxn],s[maxn],n;
int newnode(int l){
memset(to[sz],,sizeof(to[sz]));
len[sz]=l,fail[sz]=;
return sz++;
}
void init(){
sz=last=n=;
newnode();newnode(-);
s[]=-,fail[]=;
}
int getfail(int p){
while(s[n]!=s[n-len[p]-]) p=fail[p];
return p;
}
void extend(int w){
s[++n]=w;
int p=getfail(last);
if(!to[p][w]){
int np=newnode(len[p]+);
fail[np]=to[getfail(fail[p])][w];
to[p][w]=np;
}
last=to[p][w];
}
}pam; int main()
{
while(scanf("%s",&S)==){
pam.init();
int n=strlen(S);
for(int i=n-;i>=;i--){
pam.extend(S[i]-'a');
r[i]=i+pam.len[pam.last];
}
int pos=,mx=,ans=;
for(int i=;i<n;i++){
if(i>pos) pos=mx,mx=,ans++;
if(r[i]>mx) mx=r[i];
if(i==n-&&pos==n-) ans++;
}
printf("%d\n",max(,ans-));
}
return ;
}
BZOJ 3790 神奇项链 hash/后缀自动机+贪心的更多相关文章
- BZOJ 3790 神奇项链(manacher+贪心)
3790: 神奇项链 Time Limit: 10 Sec Memory Limit: 64 MB Description 母亲节就要到了,小 H 准备送给她一个特殊的项链.这个项链可以看作一个用小 ...
- BZOJ 3790: 神奇项链 [Manacher 贪心]
3790: 神奇项链 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 405 Solved: 200[Submit][Status][Discuss] ...
- bzoj 3790: 神奇项链
3790: 神奇项链 Description 母亲节就要到了,小 H 准备送给她一个特殊的项链.这个项链可以看作一个用小写字 母组成的字符串,每个小写字母表示一种颜色.为了制作这个项链,小 H 购买了 ...
- bzoj 3790 神奇项链 回文串 manacher|PAM
LINK:神奇项链 存在两个操作:1. 一个操作可以生成所有形式的回文串 2.一个操作可以将两个串给合并起来 如果前缀和后缀相同还可以将其并起来. 多组询问 每次询问合成一个串所需最少多少次2操作. ...
- BZOJ 3790 神奇项链(回文自动机+线段树优化DP)
我们预处理出来以i为结尾的最长回文后缀(回文自动机的构建过程中就可以求出)然后就是一个区间覆盖,因为我懒得写贪心,就写了线段树优化的DP. #include<iostream> #incl ...
- bzoj 3790 神奇项链(Manacher,DP+BIT | 贪心)
[题意] 你可以产生一个回文串,也可以将两个串合并成一个串,问产生目标串需要的最少合并次数. [思路] 显然我们要先产生目标串中包含的极大回文字符串. Manacher求出每个位置可以向两边延伸的最长 ...
- BZOJ 3790 神奇项链(manacher+DP+树状数组)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3790 [题目大意] 问最少用几个回文串可以构成给出串,重叠部分可以合并 [题解] 我们 ...
- BZOJ_2099_[Usaco2010 Dec]Letter 恐吓信_后缀自动机+贪心
BZOJ_2099_[Usaco2010 Dec]Letter 恐吓信_后缀自动机 Description FJ刚刚和邻居发生了一场可怕的争吵,他咽不下这口气,决定佚名发给他的邻居 一封脏话连篇的信. ...
- BZOJ 4327: JSOI2012 玄武密码 后缀自动机
Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) # ...
随机推荐
- iOS之动态计算文字的高度
+ (CGSize)boundingALLRectWithSize:(NSString *)txt Font:(UIFont *)font Size:(CGSize)size { NSMutableA ...
- 使用js函数格式化xml字符串带缩进
遇到了一个做soap的API的操作,中途需要说明xml的组装模式等, 如上图,组装产生的mxl代码药格式化并展示.由于是在前端做的,所以需要将字符串将xml进行格式化并输出,找到别人写的算法稍加更改并 ...
- 【转载】JavaScript导出Excel
[转载]JavaScript导出Excel 原文地址 如果没有用到前端插件,也没有用到后台poi导出的话,用js导出也是一种方式.亲测可用. /** * 导出excel */ var idTmr; f ...
- WSO2 API Manager 替换mysql作为数据库,解决AuthorizationUtils Could not set authorizations for the root问题
按照wso2官网(https://docs.wso2.com/display/ADMIN44x/Changing+to+MySQL)配置AM的数据库,想从H2换成Mysql5.7,费了将近一天的时间, ...
- 浅谈HashMap与线程安全 (JDK1.8)
HashMap是Java程序员使用频率最高的用于映射(键值对)处理的数据类型.HashMap 继承自 AbstractMap 是基于哈希表的 Map 接口的实现,以 Key-Value 的形式存在,即 ...
- bootstrap-paginator分页插件的简单使用实例
Document 21:36:40 简述:bootstrap-paginator是一款基于bootstrap的jQuery分页插件. githup项目地址:https://github.com/lyo ...
- 面试题——Java虚拟机
一.运行时数据区域 Java虚拟机在执行Java程序的时候会把它所管理的内存划分为若干个不同的数据区域,这些区域各有用途: 程序计数器:(线程私有的) 程序计数器是一块较小的内存,可以看作是当前线程所 ...
- 对Python语法简洁的贴切描述
很多人认为,Python与其他语言相比,具有语法简洁的特点.但这种简洁到底体现在哪些地方,很少有人能说清楚.今天看到一个对这一问题的描述,个人觉得很不错,原文如下: “Python语法主要用来精确表达 ...
- PAT A1060 (Advanced Level) Practice
If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered ...
- python教程(三)·函数与模块
函数,这和数学中的函数有点关联,但又不是完全等价 概念 不说的这么官方,我就已自己的理解来表达 ^_^ 在数学中,把一个或多个值(输入x)进行一定的计算或者映射,得到一个值(输出y),这个计算或者映射 ...