ACM题目————最长回文串
Description
回文就是正反读都是一样的字符串,如aba, abba等
Input
两组case之间由空行隔开(该空行不用处理)
字符串长度len <= 110000
Output
Sample Input
abab
Sample Output
3
用manacher算法求最长回文串长度。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <stack>
#include <cmath>
#include <string>
#include <queue>
#define INF 10000001 using namespace std;
const int maxn = *;
char str[maxn];
int p[maxn]; void manacher(char *s, int len){
p[] = ;
int Max = , d= ;
for(int i=; i<len; i++){
p[i] = Max > i ? min(p[d*-i],Max-i): ;
while( s[i+p[i]] == s[i-p[i]]) p[i] ++ ;
if( i + p[i] > d + p[d] ){
d = i ;
Max = i + p[i] ;
}
}
} int main(){
while( ~ scanf("%s",str) ){
int len = strlen(str);
for(int i=len; i>=; i--){
str[(i<<)+] = '#' ;
str[(i<<)+] = str[i] ;
}
str[] = '*' ;
len = len * + ;
manacher(str,len);
int ans = ;
for(int i=; i<len; i++){
ans = max(p[i]-,ans);
}
printf("%d\n",ans);
}
return ;
}
ACM题目————最长回文串的更多相关文章
- 字符串的最长回文串:Manacher’s Algorithm
题目链接:Longest Palindromic Substring 1. 问题描述 Given a string S, find the longest palindromic substring ...
- Manacher's Algorithm 马拉车算法(求最长回文串)
作用:求一个字符串中的最长子串,同时还可以求所有子串的长度. 题目链接: https://vjudge.net/contest/254692#problem/B 最长回文串长度的代码: int Man ...
- (最长回文串 模板) 最长回文 -- hdu -- 3068
http://acm.hdu.edu.cn/showproblem.php?pid=3068 最长回文 Time Limit: 4000/2000 MS (Java/Others) Memory ...
- Hdu 3294 Girls' research (manacher 最长回文串)
题目链接: Hdu 3294 Girls' research 题目描述: 给出一串字符串代表暗码,暗码字符是通过明码循环移位得到的,比如给定b,就有b == a,c == b,d == c,.... ...
- Leetcode0005--Longest Palindromic Substring 最长回文串
[转载请注明]http://www.cnblogs.com/igoslly/p/8726771.html 来看一下题目: Given a string s, find the longest pali ...
- Manacher(输出最长回文串及下标)
http://acm.hdu.edu.cn/showproblem.php?pid=3294 Girls' research Time Limit: 3000/1000 MS (Java/Others ...
- Manacher(最长回文串)
http://acm.hdu.edu.cn/showproblem.php?pid=3068 最长回文 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符 ...
- HDU 3068 最长回文 (Manacher最长回文串)
Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.回文就是正反读都是一样的字符串,如aba, abba等 Input 输 ...
- leetcode 每日签到 409. 最长回文串
题目: 最长回文串 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: ...
随机推荐
- iOS 在一个应用程序中调另一个应用程序
在A应用程序中调用B应用程序 1. 首先在B应用程序中生成URL 1)点击targets文件 2)点击Info 3)生成URL ①在Info.plist文件中点击+(新添加一项) ②在Info.pli ...
- Python生成8位随机密码
#!/usr/bin/env python # -*- coding: utf- -*- import random import string #第一种方法 seed = "1234567 ...
- tooltip
/* 背景色 ; 字体颜色 ; 云,显示在上面 */ .tooltip-inner{ background-color: #FF0000; ForeColor:#0f0; IsBalloon:true ...
- 对ASP.NET Cookie的一些新的认识
做用户登录,我一直用form验证的方式.有时候,为了节省时间,用户希望用户名输入框能够记住用户名,省得下次重新输入.这个时候光用form验证是不行的,因为form验证的话,用户一退出系统就失效了,所以 ...
- SQL百分比
百分比=cast(cast(count(ProvinceName)*100./ (select count(Id) from dbo.TopicCurrent where boardId=316 ...
- configs for postgresql restart and postgresql reload
-- configs requiring postgresql restart select name, setting, context from pg_settings where context ...
- 树形DP+RMQ+单调队列(Bob’s Race HDU4123)
题意:有n个房子,这些房子被n-1条道路连接,有一些运动员从一个房子为起点尽可能跑最远的距离且不能通过一条道路超过两次,这些运行员不能选择同样的起点,这些运动员跑的最远距离和最近距离的差值不能超过Q, ...
- StringBuffer类总结
package day13; /* StringBuffer是字符串缓冲区. 是一个容器. 特点: 1,长度是可变化的. 2,可以字节操作多个数据类型. 3,最终会通过toString方法变成字符串. ...
- 解析xml文件
package com.ss1.xml; import java.io.File; import java.io.FileOutputStream; import java.io.IOExceptio ...
- 2.js基础
4.函数 1)函数是一段完成“指定功能”的已经“命名”的代码段 2)函数只有“调用”才能使用到,调用就是通过名称(可以在声明之前,也可以在声明之后) 3)函数名.参数.函数体.返回值(没有返回值的函数 ...