浙大PAT甲级 1040
2016-08-22 10:13
309 查看
简单的字符串题,求最长的对称字串。分两种情况一种是奇数,一种是偶数。
AC代码:
#include<iostream>
#include<vector>
#include<map>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<list>
#include<set>
using namespace std;
int main()
{
char s[1005];
gets(s);
int maxx=1;
for(int i=0;i<strlen(s);i++)
{
int j=i-1;
int r=i+1;
int ans=1;
while(j>=0&&r<strlen(s)&&s[j]==s[r])
{
ans+=2;
j--;
r++;
}
if(ans>maxx)
{
maxx=ans;
}
}
for(int i=0;i<strlen(s);i++)
{
int j=i;
int r=i+1;
int ans=0;
while(j>=0&&r<strlen(s)&&s[j]==s[r])
{
ans+=2;
j--;
r++;
}
if(ans>maxx)
{
maxx=ans;
}
}
cout<<maxx;
}
AC代码:
#include<iostream>
#include<vector>
#include<map>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<list>
#include<set>
using namespace std;
int main()
{
char s[1005];
gets(s);
int maxx=1;
for(int i=0;i<strlen(s);i++)
{
int j=i-1;
int r=i+1;
int ans=1;
while(j>=0&&r<strlen(s)&&s[j]==s[r])
{
ans+=2;
j--;
r++;
}
if(ans>maxx)
{
maxx=ans;
}
}
for(int i=0;i<strlen(s);i++)
{
int j=i;
int r=i+1;
int ans=0;
while(j>=0&&r<strlen(s)&&s[j]==s[r])
{
ans+=2;
j--;
r++;
}
if(ans>maxx)
{
maxx=ans;
}
}
cout<<maxx;
}