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] == str[q]) {
        p--, q++;
        len += 2;
    }
    return len;
}

int main() {
    scanf("%[^\n]s", line);

    int p = 0, q = 0;
    line2[p++] = ‘ ‘;
    line2[p++] = line[q++];
    while (line[q] != ‘\0‘) {
        line2[p++] = ‘ ‘; // dummy char used as seperator
        line2[p++] = line[q++];
    }
    line2[p++] = ‘ ‘;
    line2[p] = ‘\0‘;
    int max_len = 0;
    for (int i=0; i<p; i++) {
        int cur_len = syslen(line2, i);
        if (cur_len > max_len) max_len = cur_len;
    }
    printf("%d", max_len/2);
    return 0;
}

brute force for small case

时间: 2024-12-28 22:33:28

PAT 1040 Longest Symmetric String的相关文章

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

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&

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

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

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

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.