___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 ...
随机推荐
- spring注解略解
@Scope("prototype") 如果ACTION中不@Scope("prototype"),有可能报找不到XXXACTION的错误!写上这个就表示每次请 ...
- nginx日志按日期自动切割脚本
#!/bin/bash #nginx日志切割脚本 #author:setevn #设置日志文件存放目录 logs_path="/usr/local/nginx/logs/" #设置 ...
- Xpath--使用Xpath爬取糗事百科成人版图片
#!usr/bin/env python#-*- coding:utf-8 _*-"""@author:Hurrican@file: 爬取糗事百科.py@time: 20 ...
- Linux搭建oracle数据库
1.安装前准备 软件硬件要求 操作系统:CentOS 6.4(32bit)Oracle数据库版本:Oracle 10g(10201_database_linux32.zip)最小内存:1G(检查命 ...
- Asp 6种页面转向方法
asp.net 页面转向方法其实就是两种 服务器端转向和客户端转向 客户端转向实质上是指由浏览器直接向服务器端重新发送一个请求. 而服务器端转向是指服务器内部进行页面的跳转. 服务器端转向和客户端转向 ...
- 计蒜客 宝藏 (状压DP)
链接 : Here! 思路 : 状压DP. 开始想直接爆搜, T掉了, 然后就采用了状压DP的方法来做. 定义$f[S]$为集合$S$的最小代价, $dis[i]$则记录第$i$个点的"深度 ...
- C++ STL-stack使用详解
stack 类是容器适配器,它给予程序员栈的功能--特别是 FILO (先进后出)数据结构. 该类模板表现为底层容器的包装器--只提供特定函数集合.栈从被称作栈顶的容器尾部推弹元素. 一:头文件 #i ...
- 阻止a标签跳页,使用单击事件函数处理该请求
阻止a标签跳页href="javascript:void(0)" 单击该标签时 页面不跳页 使用单击事件函数来处理该单击请求 返回上一层页面 ---------------- ...
- 教你十分钟构建好 SpringBoot + SSM 框架
目前最主流的 java web 框架应该是 SSM,而 SSM 框架由于更轻便与灵活目前受到了许多人的青睐.而 SpringBoot 的轻量化,简化项目配置, 没有 XML 配置要求等优点现在也得到了 ...
- HDU5976 Detachment
/* HDU5976 Detachment http://acm.hdu.edu.cn/showproblem.php?pid=5976 数论 等差数列 * * */ #include <cst ...