___Manacher(线性回文子串处理算法)
昨晚的bc做得好忧郁-----
第一题改了好久好久好久----等改完发现比赛已经结束了(发现是枚举子集的位运算那儿写错了--)
第二题是判断能否将一个字符串划分成三段回文串
今天学了一点点 Manacher
http://wenku.baidu.com/view/3031d2d3360cba1aa811da42.html
模板大概是这样的--
void Manacher(){
for(int i = ;i <= len;i++){
t[*i-] = '#';
t[*i] = s[i];
}
t[] = '?';t[len*+] = '#';
t[*len+] = '\0';
int tmax = ,id = ;
len = len* + ;
for(int i = ;i <= len;i++){
if(tmax > i) p[i] = min(p[*id-i],tmax-i);
else p[i] = ;
while(t[i-p[i]] == t[i+p[i]]) p[i]++;
if(i+p[i] > tmax){
tmax = i+p[i];
id = i;
}
}
}
hdu 3068
求最长回文串
#include <cstdio>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std; #define getmid(l,r) ((l) + ((r) - (l)) / 2)
#define MP(a,b) make_pair(a,b)
#define PB push_back typedef long long ll;
typedef pair<int,int> pii;
const double eps = 1e-;
const int INF = ( << ) - ;
const int maxn = *; char s[maxn];
char t[maxn];
int p[maxn]; void Manacher(){
int len = strlen(s+);
for(int i = ;i <= len;i++){
t[*i-] = '#';
t[*i] = s[i];
}
t[] = '?';
t[*len+] = '#';
t[*len+] = '\0';
int tmax = ,id = ;
int ans = ;
len = *len + ;
for(int i = ;i <= len;i++){
if(tmax > i) p[i] = min(p[*id-i],tmax-i);
else p[i] = ;
while(t[i-p[i]] == t[i+p[i]]) p[i]++;
if(i+p[i] > tmax){
tmax = i+p[i];
id = i;
}
ans = max(ans,p[i]);
}
printf("%d\n",ans-);
} int main(){
while(scanf("%s",s+) != EOF){
Manacher();
}
return ;
}
hdu 5340
先用一遍Manacher
然后如果从1到这个位置是回文串的话,把这个位置加进L[]数组
如果从这个位置到结尾是回文串的话,把这个位置加进R[]数组
再枚举L,R,判断L---R这个区间是不是回文串
判断方法就是看一下,这个区间的中点的p[i]能否覆盖这个中间部分---
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std; const int maxn = ; char s[maxn],t[maxn];
int p[maxn],L[maxn],R[maxn];
int len; void Manacher(){
for(int i = ;i <= len;i++){
t[*i-] = '#';
t[*i] = s[i];
}
t[] = '?';t[len*+] = '#';
t[*len+] = '\0';
int tmax = ,id = ;
len = len* + ;
for(int i = ;i <= len;i++){
if(tmax > i) p[i] = min(p[*id-i],tmax-i);
else p[i] = ;
while(t[i-p[i]] == t[i+p[i]]) p[i]++;
if(i+p[i] > tmax){
tmax = i+p[i];
id = i;
}
}
} bool solve(){
Manacher();
int l = ,r = ;
int length = strlen(s+);
for(int i = ;i <= len-;i++){
if(i - p[i] == ) L[l++] = i;
if(i + p[i] == len+) R[r++] = i;
} for(int i = ;i < l;i++){
for(int j = r-;j >= ;j--){
// printf("R[%d] = %d ",j,R[j]);
int lb = L[i] + p[L[i]],ub = R[j] - p[R[j]];
if(lb > ub) break;
int mid = (lb + ub)/;
// printf("lb = %d ub = %d mid = %d\n",lb,ub,mid);
if(p[mid] > mid - lb) return true;
}
}
return false;
} int main(){
int T;
scanf("%d",&T);
while(T--){
memset(p,,sizeof(p));
scanf("%s",s+);
len = strlen(s+);
if(solve()) printf("Yes\n");
else printf("No\n");
}
return ;
}
题解的暴力压位还是不会的说啊~~~
___Manacher(线性回文子串处理算法)的更多相关文章
- 九度OJ 1528 最长回文子串 -- Manacher算法
题目地址:http://ac.jobdu.com/problem.php?pid=1528 题目描述: 回文串就是一个正读和反读都一样的字符串,比如"level"或者"n ...
- lintcode最长回文子串(Manacher算法)
题目来自lintcode, 链接:http://www.lintcode.com/zh-cn/problem/longest-palindromic-substring/ 最长回文子串 给出一个字符串 ...
- 最长回文子串Manacher算法模板
Manacher算法能够在O(N)的时间复杂度内得到一个字符串以任意位置为中心的回文子串.其算法的基本原理就是利用已知回文串的左半部分来推导右半部分. 首先,在字符串s中,用rad[i]表示第i个字符 ...
- 最长回文子串—Manacher 算法 及 python实现
最长回文子串问题:给定一个字符串,求它的最长回文子串长度.如果一个字符串正着读和反着读是一样的,那它就是回文串. 给定一个字符串,求它最长的回文子串长度,例如输入字符串'35534321',它的最 ...
- hihocoder #1032 : 最长回文子串 Manacher算法
题目链接: https://hihocoder.com/problemset/problem/1032?sid=868170 最长回文子串 时间限制:1000ms内存限制:64MB 问题描述 小Hi和 ...
- 5. Longest Palindromic Substring(最长回文子串 manacher 算法/ DP动态规划)
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- HiHo 1032 最长回文子串 (Manacher算法求解)
/** * 求解最长回文字串,Manacher算法o(n)求解最长回文子串问题 **/ #include<cstdio> #include<cstdlib> #include& ...
- hihoCoder #1032 : 最长回文子串 [ Manacher算法--O(n)回文子串算法 ]
传送门 #1032 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相 ...
- 51nod1089 最长回文子串 manacher算法
0. 问题定义 最长回文子串问题:给定一个字符串,求它的最长回文子串长度. 如果一个字符串正着读和反着读是一样的,那它就是回文串.下面是一些回文串的实例: 12321 a aba abba aaaa ...
随机推荐
- 实现数组类(C++ 拷贝构造函数、拷贝函数)要判断赋值左右对象不相等,坑惨了
#include <iostream> using namespace std; class ArrayIndexOutOfBoundsException{ // 异常类 public: ...
- 如何分页爬取数据--beautisoup
'''本次爬取讲历史网站'''#!usr/bin/env python#-*- coding:utf-8 _*-"""@author:Hurrican@file: 分页爬 ...
- Javascript解析URL
举个栗子,一个网页的URL为https://i.cnblogs.com/EditPosts.aspx?opt=1,要分离出通信协议.host.port.path.query.hash等值.这时候我们应 ...
- 【JavaScript框架封装】实现一个类似于JQuery的缓存框架的封装
// 缓存框架 (function (xframe) { /** * 实现了缓存框架的临时存储功能(内存存储) * @type {{data: Array, get: (function(*): *) ...
- vue 配置页面动态的 title
- Linux设备驱动--块设备(四)之“自造请求”(转)
前面, 我们已经讨论了内核所作的在队列中优化请求顺序的工作; 这个工作包括排列请求和, 或许, 甚至延迟队列来允许一个预期的请求到达. 这些技术在处理一个真正的旋转的磁盘驱动器时有助于系统的性能. 但 ...
- Python hangman小游戏
hangman # words.py 使用pickle永久性存储数据 import pickle filename = 'words.pk' data = ['cat', 'dog', 'perro' ...
- 如何在 VMware 上安装 CentOS 6.8
一.下载 CentOS 6.8 64位 链接:https://pan.baidu.com/s/15qmWGar2m0WzsWxDk4tSSg 密码:wqra 二.安装步骤 1.新建虚拟机 2.自定义 ...
- 解决VTune错误PMU resources currently being used by another profiling tool or process
错误信息: When I ran Hardware Event-based Sampling Analysis 0, it showed the ERROR: Collection failed Co ...
- HDU 3886
一开始又往打表想了....不过,打表确实不好处理,转DFS. DFS有几个要注意的问题,1.对于枚举以零开始的数.我纠结了很久,最终学习别人的方法,设一个BOOL,并且假设最高一位有零,很方便.2.当 ...