ural 1297 Palindrome(Manacher模板题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
求最长回文子串。
http://acm.timus.ru/problem.aspx?space=1&num=1297
Manacher模板题,复杂度O(n),做这题纯属是为了验一下自己写的模板是否正确。
当然这题也可以用后缀数组来搞
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
#define MAXN 100010
char str[MAXN],s[MAXN];
int p[MAXN];
int n;
void Manacher(){
n=strlen(s);
str[]='$';
str[]='#';
for(int i=;i<n;i++)str[*i+]=s[i],str[*i+]='#';
n=n*+;
str[n]=;
int mx=,id;
for(int i=;i<n;i++){
if(mx>i)p[i]=min(p[*id-i],mx-i);
else p[i]=;
for(;str[i+p[i]]==str[i-p[i]];p[i]++);
if(p[i]+i>mx)mx=p[i]+i,id=i;
}
} int main()
{
ios::sync_with_stdio(false);
while(scanf("%s",s)!=EOF){
Manacher();
int ans=;
int id;
int maxx=;
for(int i=;i<n;i++){
if(p[i]>maxx)id=i,maxx=p[i];
}
//cout<<maxx<<endl;
p[id]--;
for(int i=id-p[id];i<=id+p[id];i++){
if(str[i]=='#')continue;
printf("%c",str[i]);
}
puts("");
}
return ;
}
ural 1297 Palindrome(Manacher模板题)的更多相关文章
- HDU 3068 最长回文( Manacher模板题 )
链接:传送门 思路:Manacher模板题,寻找串中的最长回文子串 /***************************************************************** ...
- Manacher Ural 1297 Palindrome
1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB The “U.S. Robots” HQ has just received a ...
- Ural 1297 Palindrome(Manacher或者后缀数组+RMQ-ST)
1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a ...
- URAL 1297 Palindrome(Manacher)
The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent ...
- URAL 1297 Palindrome 后缀数组
D - Palindrome Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Subm ...
- 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome
题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...
- 最长回文 HDU - 3068 manacher 模板题
题意:找串的最长回文字串(连续) 题解:manacher版题 一些理解:首位加上任意两个字符是为了判断边界. 本算法主要是为了 1.省去奇偶分类讨论. 2.防止形如aaaaaaa的串使得暴力算法蜕化为 ...
- HDU 3068 最长回文(manacher模板题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 题目大意:求字符串s中最长的回文子串 解题思路:manacher模板 代码 #include&l ...
- URAL - 1297 Palindrome —— 后缀数组 最长回文子串
题目链接:https://vjudge.net/problem/URAL-1297 1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB ...
随机推荐
- C++中的数组和指针
#include <iostream> #include <set> using namespace std; int main() { ] = {,,,,,}; ]; p = ...
- Render和template?
Template是一个模板. render = web.template.render('templates/') 这会告诉web.py到你的模板目录中去查找模板.然后把 index.GET改成: 告 ...
- C#通过文件路径截取对应的文件夹路径
try { OpenFileDialog openFileDialog = new OpenFileDialog(); string str = comboBox_hexFilePath.Text ...
- Python Tcp Socket
socket(套接字),传输层通信的端点,由IP和端口号组成(IP,Port),可以通过socket精确地找到服务器上的进程并与之通信 python2.6实现,基于AF_INET(网络套接字) 类型S ...
- spring的数据源基本配置
aplictaionContext-dataSource的配置: <?xml version="1.0" encoding="utf-8"?> &l ...
- 关于qt学习的一点小记录(1)
今日为了应付学校作业要求 决定现学qt来制作界面 毕竟c++不像在这方面c#可以那么方便 qt主要依靠信号.槽来实现类似winform中的消息 鉴于要尽快做完,故而没有细看qt 只是大概了解了下界面的 ...
- JSplitPane详解
摘自http://blog.163.com/xiexueyong1987@126/blog/static/1262673422010102711295541/ JSplitPane详解 pasting ...
- SQL生成随机字符串
1.SQLserve生成随机字符串 SELECT replace(newid(), '-', '')
- jsp判断session中的值
方法有两种: 假设,此session的名字叫adminAccount 1.EL表达式 <script type="text/javascript"> if($.trim ...
- execl csv导出
方维js方法:function export_csv() { var inputs = $(".search_row").find("input:[type!='chec ...