Manacher--雾窗寒对遥天暮,暮天遥对寒窗雾
POJ 3974: Palindrome
题意:
最长回文子串的长度...
分析:
Manacher板子题...
代码:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
//by NeighThorn
using namespace std;
//眉眼如初,岁月如故 const int maxn=+; int cas,len,p[maxn]; char s[maxn],str[maxn]; inline void prework(void){
int i=;
for(i=;s[i];i++)
str[i*+]='#',str[(i+)*]=s[i];
len=i*+;str[]='$';str[len]=str[len+]='#';
} inline void manacher(void){
int id,mx=,ans=;
for(int i=;i<=len;i++){
p[i]=i<mx?min(mx-i,p[id*-i]):;
while(str[i+p[i]]==str[i-p[i]])
p[i]++;
if(p[i]+i>mx)
mx=p[i]+i,id=i;
if(ans<p[i]-)
ans=p[i]-;
}
printf("%d\n",ans);
} signed main(void){cas=;
while(scanf("%s",s)&&s[]!='E'){
printf("Case %d: ",++cas);
prework();manacher();
}
return ;
}//Cap ou pas cap. Pas cap.
HDU 3948: The Number of Palindromes
题意:
求原串中本质不同的子串个数...
分析:
写完发现大家都用的后缀数组...然而我不会...只能用Manacher+Hash水...
本质不同的回文串个数级别是O(n)的...所以我们可以暴力扫描回文串...我们枚举回文串的中心位置,然后判断最长回文串,如果没有出现过则判断其子串,否则直接break...
代码:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
//by NeighThorn
#define int long long
#define II int
using namespace std;
//眉眼如初,岁月如故 const II maxn=+,Mod=; int l,cas,len,p[maxn],po[maxn],Hash[maxn]; char s[maxn],str[maxn]; struct M{
int hd[Mod+],nxt[maxn],w[][maxn],cnt; inline void clear(void){
memset(hd,-,sizeof(hd));cnt=;
} inline void insert(int x,int l,int r){
w[][cnt]=l,w[][cnt]=r,nxt[cnt]=hd[x],hd[x]=cnt++;
} inline bool find(int x,int l,int r){//cout<<"find"<<endl;cout<<l<<" "<<r<<endl;
for(int i=hd[x];i!=-;i=nxt[i])
if(r-l+==w[][i]-w[][i]+){
int j;
for(j=;j<=r-l+;j++)
if(str[w[][i]+j-]!=str[w[][i]+j-])
break;
if(j==r-l+)//{
return true;//cout<<"finish find"<<endl;}
}//cout<<"finish find"<<endl;
return false;
} }mp; inline void prework(void){
int i=;l=strlen(s);
for(i=;s[i];i++)
str[i*+]='#',str[(i+)*]=s[i];
len=i*+;str[]='$';str[len]=str[len+]='#';
for(int i=;i<=len;i++){
if(str[i]>='a'&&str[i]<='z')
Hash[i]=(Hash[i-]*%Mod+str[i]-'a'+)%Mod;
else
Hash[i]=(Hash[i-]*%Mod+)%Mod;
}
} inline void manacher(void){
int id,mx=;
for(int i=;i<=len;i++){
p[i]=i<mx?min(mx-i,p[(id<<)-i]):;
while(str[p[i]+i]==str[i-p[i]])
p[i]++;
if(i+p[i]>mx)
mx=i+p[i],id=i;
}
} inline void find(void){
int ans=;
for(int i=;i<=len;i++){//cout<<i<<" "<<p[i]<<endl;
if(!mp.find((Hash[i+p[i]-]-Hash[i-]*po[p[i]]%Mod+Mod)%Mod,i,i+p[i]-)){
ans++,mp.insert((Hash[i+p[i]-]-Hash[i-]*po[p[i]]%Mod+Mod)%Mod,i,i+p[i]-);
int lala=p[i]-;
while(lala>=&&!mp.find((Hash[i+lala-]-Hash[i-]*po[lala]%Mod+Mod)%Mod,i,i+lala-))
ans++,mp.insert((Hash[i+lala-]-Hash[i-]*po[lala]%Mod+Mod)%Mod,i,i+lala-),lala-=;
}
}
printf("%lld\n",ans-);
} signed main(void){
scanf("%lld",&cas);po[]=;Hash[]=;
for(int i=;i<=;i++)
po[i]=po[i-]*%Mod;
for(int i=;i<=cas;i++){mp.clear();
scanf("%s",s);printf("Case #%lld: ",i);
prework();manacher();find();
}
return ;
}//Cap ou pas cap. Pas cap.
By NeighThorn
Manacher--雾窗寒对遥天暮,暮天遥对寒窗雾的更多相关文章
- Unity中雾效的开启
原文:https://blog.csdn.net/Rhett_Yuan/article/details/54425236 1.对于雾效的开启在新版的Unity中通过界面菜单Windows->Li ...
- paper 100:何恺明经典去雾算法
一:由简至美的最佳论文(作者:何恺明 视觉计算组) [视觉机器人:个人感觉学习他的经典算法固然很重要,但是他的解决问题的思路也是非常值得我们学习的] 那是2009年4月24日的早上,我收到了一封不同 ...
- NeHe OpenGL教程 第十六课:雾
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- 【Unity Shaders】Unity里的雾效模拟
写在前面 熟悉Unity的都知道,Unity可以进行基本的雾效模拟.所谓雾效,就是在远离我们视角的方向上,物体看起来像被蒙上了某种颜色(通常是灰色).这种技术的实现实际上非常简单,就是根据物体距离摄像 ...
- OpenCV导向滤波(引导滤波)实现(Guided Filter)代码,以及使用颜色先验算法去雾
论文下载地址:http://research.microsoft.com/en-us/um/people/jiansun/papers/GuidedFilter_ECCV10.pdf 本文主要介绍导向 ...
- 雾霾天出行,如何精确避开“雷区”?2016 SODA数据侠十强
(2016年参加了上海 SODA 竞赛,进入前十,最终获得上海市的两个奖项.) ▍跟踪雾霾,仅靠零星的监测点数据怎么行? 如果雾霾短期内没有办法彻底根治,我们可以做什么,把环境污染物对人的影响尽可能降 ...
- 电子模块 001 --- 遥杆 JoyStick
电子模块 001 - 遥杆 JoyStick - Ongoing - 2016年8月31日 星期三 遥杆 JoyStick 模块 今天介绍:JoyStick 电子模块. 模块名称: 双轴按键摇杆 PS ...
- 批处理命令——for
[1]for命令简介 先把for循环与for命令类比一下,这样学习理解快. for 循环语句,一般格式如下: for (表达式1;表达式2;表达式3) { 循环体; } 1. 表达式1 一般为初始状态 ...
- windows批处理学习(for和字符串)---03
[1]for命令简介 先把for循环与for命令类比一下,这样学习理解快. for 循环语句,一般格式如下: 1 for (表达式1;表达式2;表达式3) 2 { 3 循环体; 4 } 1. 表达式1 ...
随机推荐
- shell数组脚本
#!/bin/bash array=( ) ;i<${#array[*]};i++)) do echo ${array[i]} done 脚本2 #!/bin/bash array=( ) fo ...
- PHP静态文件缓存
ob_start(); 2 echo 'aaa'; 3 $string = ob_get_contents(); 4 file_put_contents('a.html', $string); 5 o ...
- 使用Navicat连接阿里云ECS服务器上的MySQL数据库
一.首先要mysql授权 mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '你的mysql数据库密码' WITH GR ...
- Susan Sontag【苏珊·桑塔格】
Sunsan Sontag Sunsan Sontag was one of the most noticeable figures in the world of literature. 苏珊·桑塔 ...
- FZU 2082 过路费(树链剖分)
树链剖分模板题. FZU炸了,等交上去AC了再贴代码.
- Codeforces Round #460 (Div. 2)-A. Supermarket
A. Supermarket time limit per test2 seconds memory limit per test256 megabytes Problem Description W ...
- DFS:POJ1190-生日蛋糕(基础搜索)
生日蛋糕 Time Limit: 1000MS Memory Limit: 10000K 描述 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体. ...
- 为什么要用枚举实现Singleton--Java
原文地址:http://www.cnblogs.com/AprilCal/p/5426007.html 理由一:无需再考虑可序列化的情况 <effective java>第77条:对于 ...
- 给B公司的一些建议(又一篇烂尾的文章)
感慨:太多太多的悲伤故事,发生在自己身上,发生在自己的身边.因此,为了避免总是走"弯路",走"错误"的道路,最近一直在完善自己的理论模型. 烂尾说明:本文是一篇 ...
- 线程、进程、队列、IO多路模型
操作系统工作原理介绍.线程.进程演化史.特点.区别.互斥锁.信号.事件.join.GIL.进程间通信.管道.队列.生产者消息者模型.异步模型.IO多路复用模型.select\poll\epoll 高性 ...