您的位置:首页 > 其它

浙大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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: