1040 the longest symmetric

以每个字符为中心,分两种情况向两边扩展。

manacher算法参见http://www.cnblogs.com/houkai/p/3371807.html

AC代码:

#include <string>
#include <cstdio>
#include <iostream>
using namespace std;
int main(){
    string s;
    getline(cin,s);
    int maxlen(0);
    for(int i = 0;i < s.size();i++){
        int m(i - 1),n(i + 1);
        for(;m >= 0 && n < s.size();m--,n++){
            if(s[m] != s[n]){
                m++;
                n--;
                break;
            }
        }
        if(n == s.size() || m < 0){
            m++;
            n--;
        }
        if(n - m + 1 > maxlen)
            maxlen = n - m + 1;
        m = i;
        n = i + 1;
        for(;m >= 0 && n < s.size();m--,n++){
            if(s[m] != s[n]){
                m++;
                n--;
                break;
            }
        }
        if(n == s.size() || m < 0){
            m++;
            n--;
        }
        if(n - m + 1 > maxlen)
            maxlen = n - m + 1;
    }
    printf("%d\n",maxlen);
    return 0;
}
时间: 2024-10-17 20:40:21

1040 the longest symmetric的相关文章

PAT 1040. Longest Symmetric String (25)

1040. Longest Symmetric String (25) Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given "Is PAT&TAP symmetric?", the longest symmetric sub-string is "s PAT&TAP s", hence

PAT 甲级 1040 Longest Symmetric String (25 分)(字符串最长对称字串,遍历)

1040 Longest Symmetric String (25 分) Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given Is PAT&TAP symmetric?, the longest symmetric sub-string is s PAT&TAP s, hence you must output 11. In

1040. Longest Symmetric String (25)

题目例如以下: Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given "Is PAT&TAP symmetric?", the longest symmetric sub-string is "s PAT&TAP s", hence you must output 11. Input S

pat1040. Longest Symmetric String (25)

1040. Longest Symmetric String (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given "Is PAT&TAP symmetric?", th

浙大pat1040 Longest Symmetric String(25 分)

1040 Longest Symmetric String(25 分) Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given Is PAT&TAP symmetric?, the longest symmetric sub-string is s PAT&TAP s, hence you must output 11. Inp

PAT1040. Longest Symmetric String

Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given "Is PAT&TAP symmetric?", the longest symmetric sub-string is "s PAT&TAP s", hence you must output 11. Input Specifica

PAT甲级——A1040 Longest Symmetric String

Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given Is PAT&TAP symmetric?, the longest symmetric sub-string is s PAT&TAP s, hence you must output 11. Input Specification: Each input file co

PAT (Advanced Level) 1040. Longest Symmetric String (25)

暴力. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; char s[2000]; int ans,len,sum; void check(int a,int b) { if(s[a]!=s[b]) return; if(a==b) sum=1; else sum=2; int left=a-1,right=b+1; while(!(left<0||right&

PAT 1040 Longest Symmetric String

#include <cstdio> #include <cstdlib> using namespace std; char line[1001]; char line2[2003]; int syslen(char str[], int start) { int len = 1; int p = start - 1; int q = start + 1; while (p >=0 && str[q] != '\0' && str[p] ==