hdu5340Three Palindromes 最长字符串

//用manacher算法得到p[i]表示以该点为中心的回文串最长能延伸p[i]长度
//通过p[i]可以求得其pre[i] , suf[i]表示以i为最后一个前面,后面是否能形成回文串
//枚举中间的字符串的位置和长度得到答案
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std ;
const int maxn = 40010 ;
char str[maxn] ;
char s[maxn] ;
int p[maxn] ;
int pre[maxn] ;
int suf[maxn] ;
int n ;
void pk()
{
    int i;
    int mx = 0;
    int id;
    for(i=1; i<n; i++)
    {
        if( mx > i )
            p[i] = min( p[2*id-i], mx-i );
        else
            p[i] = 1;
        for(; str[i+p[i]] == str[i-p[i]]; p[i]++)
            ;
        if( p[i] + i > mx )
        {
            mx = p[i] + i;
            id = i;
        }
    }
}
bool solve()
{
    for(int i = 4;i < n - 3;i++)
    for(int j = (str[i] == ‘#‘ ? 2 : 1);j <= p[i] ;j++)
    if(i - j == 1 || i + j == n-1)continue ;
    else if(pre[i - j]&suf[i + j])
    return true ;
    return false ;
}
int main()
{
    //freopen("in.txt" ,"r" , stdin) ;
    int T ;
    scanf("%d" ,&T) ;
    while(T--)
    {
        scanf("%s" , s) ;
        int len = strlen(s) ;
        n = 1;
        for(int i = 0; i < len ;i++)
        {
            str[n++] = ‘#‘ ;
            str[n++] = s[i] ;
        }
        str[n++] = ‘#‘ ;
        pk() ;
        int ma = 0;
        memset(pre , 0 , sizeof(pre));
        memset(suf ,0 , sizeof(suf)) ;
        for(int  i = 1 ;i < n ;i++)
        {
            if(i == p[i])pre[i + p[i] - 1] = 1;
            if(p[i] == (n-i))suf[i - p[i] + 1] = 1;
        }
        if(solve())puts("Yes");
        else puts("No") ;
    }
    return 0 ;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-31 14:58:19

hdu5340Three Palindromes 最长字符串的相关文章

JSP简单练习-省略显示长字符串

<%@ page contentType="text/html; charset=gb2312" %> <!-- JSP指令标签 --> <%@ page import="java.util.*" %> <!-- JSP指令标签 --> <html> <head> <title>长字符串截取示例</title> </head> <body> <

C语言 &#183; 最长字符串

算法训练 最长字符串 时间限制:1.0s   内存限制:512.0MB 求出5个字符串中最长的字符串.每个字符串长度在100以内,且全为小写字母. 样例输入 one two three four five 样例输出 three 作者注释: 有一个疑问:代码第九行,为何 若str定义为一维数组就会报错? 1 #include<stdio.h> 2 #include<string.h> 3 int main(){ 4 char str[5][100]; 5 int max=0;//记录

Jquery-截取过长字符串

页面获取的字符串数据过长,导致显示的表格排列不整齐,这是就要对表格中过长的字符串进行处理之后显示. 方法一:(别的博客看到的方法) js方法: <script type="text/javascript"> jQuery.fn.limit=function(){ var self = $(".table tr td"); self.each(function(){ var objString = $(this).text(); var objLength

【Py】变量、字符串、原始字符串、长字符串

变量: python中变量不需要事先声明,但在使用之前需要赋值,和其他编程语言一样由字母.数字.下划线组成,且首位不能是数字 字符串: python中是用双引号或单引号括起来的内容,但单双引号不能混用,如过在字符串中本身又包含字符串,可以使用转义字符 Let's go!的两种写法: 1 >>> 'Let\'s go!' 2 "Let's go!" 1 >>> "Let's go!" 2 "Let's go!"

在一个字符串寻找另一个字符串,并且输出短字符串头字母在长字符串的下标

录入两个字符串,一个字符串是另一个字符串的字串,输出子串首字母在长字符串的位置. 两个解决办法 使用库函数strncmp(str1,str2,n); 功能:比较str1和str2两个字符串的前n个字母,一一比较,若str1大则返回1,若str1和str2相等,则返回0,否则返回-1: int compare(char *str1, char *str2) { int i; int lenstr1 = strlen(str1); int lenstr2 = strlen(str2); if( le

php将长字符串拆分为指定最大宽度的字符串数组

/** * 将字符串拆分为指定最大宽度的字符串数组.单字节字符宽度为1,多字节字符通常宽度为2 * @param string $msg 要拆分的字符串 * @param int $width 结果数组中每个元素的最大宽度.如10代表10个英文字母或者5个汉字 * @param string $enc 字符串编码 */ function split_str2array_cn($msg,$width,$enc = 'utf-8'){ $msg_width = mb_strwidth($msg,$e

2014年阿里巴巴在线笔试题-第3大题-公共最长字符串长度

说明 2014年阿里巴巴在线笔试题-第3大题    首先,我没参加这次的阿里巴巴在线笔试题,题目全部是从别人口中描述而来,对于以下的分析,如果有什么不对的地方还望指教.也希望大家能够有更好的办法,希望大家来能不吝赐教. 题目描述 给定一个主字符串和一个匹配字符串,现在问你,找出 "主串中可匹配到的匹配串中子串的最大长度",可能比较绕,举个例子吧 主字符串       abcdefgsdff     记为A 匹配字符串   abefgf               记为B 要求的值就是 

CSS word-wrap强制换行截断长字符串

<html> <head> <title>CSS截断长字符串,强制换行</title> </head> <style> .wordWrap{word-wrap:break-word; word-break:break-all;} </style> <body> <div class="wordWrap">rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrffffffff

YTU 2419: C语言习题 等长字符串排序

2419: C语言习题 等长字符串排序 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 426  Solved: 169 [Submit][Status][Web Board] Description 在主函数中输入n(n<=10)个等长的字符串.用另一函数对它们排序.然后在主函数输出这n个已排好序的字符串. Input n和n个等长字符串 Output n个已排好序的字符串 Sample Input 5 abcdf 12345 ert45 fg4