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>=len||s[left]!=s[right]))
    left--,right++,sum=sum+2;
  ans=max(ans,sum);
}

int main()
{
  gets(s);
  len=strlen(s); ans=0;
  for(int i=0;i<len;i++) check(i,i),check(i,i+1);
  printf("%d\n",ans);
  return 0;
}
时间: 2024-10-25 11:20:02

PAT (Advanced Level) 1040. Longest Symmetric String (25)的相关文章

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

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] ==

PAT (Advanced Level) 1074. Reversing Linked List (25)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<algorithm> using namespace std; const int maxn=100000+10

PAT (Advanced Level) 1062. Talent and Virtue (25)

简单排序.题意较长. #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<vector> #include<string> #include<stack> #include<map> #include<algorithm> using namespace std; struct X { int id;

PAT (Advanced Level) 1060. Are They Equal (25)

模拟题.坑点较多. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using namespace

PAT (Advanced Level) 1056. Mice and Rice (25)

简单模拟. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using namespace std