昨晚的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(线性回文子串处理算法)的更多相关文章

  1. 九度OJ 1528 最长回文子串 -- Manacher算法

    题目地址:http://ac.jobdu.com/problem.php?pid=1528 题目描述: 回文串就是一个正读和反读都一样的字符串,比如"level"或者"n ...

  2. lintcode最长回文子串(Manacher算法)

    题目来自lintcode, 链接:http://www.lintcode.com/zh-cn/problem/longest-palindromic-substring/ 最长回文子串 给出一个字符串 ...

  3. 最长回文子串Manacher算法模板

    Manacher算法能够在O(N)的时间复杂度内得到一个字符串以任意位置为中心的回文子串.其算法的基本原理就是利用已知回文串的左半部分来推导右半部分. 首先,在字符串s中,用rad[i]表示第i个字符 ...

  4. 最长回文子串—Manacher 算法 及 python实现

    最长回文子串问题:给定一个字符串,求它的最长回文子串长度.如果一个字符串正着读和反着读是一样的,那它就是回文串.   给定一个字符串,求它最长的回文子串长度,例如输入字符串'35534321',它的最 ...

  5. hihocoder #1032 : 最长回文子串 Manacher算法

    题目链接: https://hihocoder.com/problemset/problem/1032?sid=868170 最长回文子串 时间限制:1000ms内存限制:64MB 问题描述 小Hi和 ...

  6. 5. Longest Palindromic Substring(最长回文子串 manacher 算法/ DP动态规划)

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  7. HiHo 1032 最长回文子串 (Manacher算法求解)

    /** * 求解最长回文字串,Manacher算法o(n)求解最长回文子串问题 **/ #include<cstdio> #include<cstdlib> #include& ...

  8. hihoCoder #1032 : 最长回文子串 [ Manacher算法--O(n)回文子串算法 ]

    传送门 #1032 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相 ...

  9. 51nod1089 最长回文子串 manacher算法

    0. 问题定义 最长回文子串问题:给定一个字符串,求它的最长回文子串长度. 如果一个字符串正着读和反着读是一样的,那它就是回文串.下面是一些回文串的实例: 12321 a aba abba aaaa ...

随机推荐

  1. Block Functionality

    Block Functionality A block is an anonymous inline collection of code that: Has a typed argument lis ...

  2. lambda表达式、匿名函数

    lambda表达式是函数式编程中的匿名函数语法规范. In computer programming, an anonymous function (function literal, lambda ...

  3. Swift - what's the difference between metatype .Type and .self?

    Declaration typealias AnyClass = AnyObject.Type .Type The metatype of a class, structure, or enumera ...

  4. JavaScript进阶【一】JavaScript模块化开发的基础知识

    //模块化的最初写法 //1.最初写法 //下面的m1和m2就组成了一个模块 //缺点:"污染"了全局变量,无法保证不与其他模块发生变量名冲突,而且模块成员之间看不出直接关系. f ...

  5. 30分钟精通React今年最劲爆的新特性——React Hooks

    你还在为该使用无状态组件(Function)还是有状态组件(Class)而烦恼吗? --拥有了hooks,你再也不需要写Class了,你的所有组件都将是Function. 你还在为搞不清使用哪个生命周 ...

  6. python第三周:集合、函数、编码、文件

    1.集合: 集合的创建: list_1 = set([1,2,3,4,5]) list_2 = set([2,3,44,7,8]) 集合的特性:集合是无序的,集合可以去掉重复的元素 集合的操作:求交集 ...

  7. Fiddler 无法监测WCF通信疑问

    别人的可以检测到通信,我的为什么不行呢? 使用的是basicHttp协议,应该可以的啊,着的是非常奇怪

  8. rabbitMQ学习笔记(七) RPC 远程过程调用

    关于RPC的介绍请参考百度百科里的关于RPC的介绍:http://baike.baidu.com/view/32726.htm#sub32726 现在来看看Rabbitmq中RPC吧!RPC的工作示意 ...

  9. [Linux]第二部分-linux文件磁盘格式

    账户信息在/etc/passwd中,密码在/etc/shadow中,组信息在etc/group中 (d/-)rwxrwxrwx 1 root 293 Oct 19 21:24 test 文件属性 连接 ...

  10. hdu 1542 线段树之扫描线之面积并

    点击打开链接 题意:给你n个矩形,求它们的面积,反复的不反复计算 思路:用线段树的扫描线完毕.将X坐标离散化后,从下到上扫描矩形,进行各种处理,看代码凝视把 #include <stdio.h& ...