ACM问题 这个题的输入问题怎么解决?? http://acm.swjtu.edu.cn/JudgeOnline/showproblem?problem_id=139
http://acm.swjtu.edu.cn/JudgeOnline/showproblem?problem_id=1399Apalindromeisastringwh...
http://acm.swjtu.edu.cn/JudgeOnline/showproblem?problem_id=1399
A palindrome is a string which reads the same forwards as it does backwards—e.g., gig, radar, and peep. Write a program which inputs a string of at most 1000 letters from the alphabet {a, b, c, . . . , z} and outputs a longest substring which is a palindrome.
Input
The standard input file contains a single string of lowercase letters of length at most 1000. The string might be split onto more than one line, but in that case the individual lines should be taken as a single string, ignoring line separators (a.k.a. carriage returns, new lines).
Output
Output a longest substring of the input which is a palindrome. If there are two or more equally long palindrome substrings, output the leftmost one.
Sample Input
bahuibyasfhbhuahsfbuhuashfbuabcdedcbakasjfkjasjaskjajhuye
Sample Output
A longest palindrome substring is:
abcdedcba
我的代码:
#include<cstdio>
#include<cstring>
#include<cctype>
#include<ctime>
#define MAXN 1000+10
char buf[MAXN], s[MAXN];
int p[MAXN];
int main()
{
int n, m=0, max=0, x, y, i, j;
while(gets(buf))
{
n=strlen(buf);
for (i=0; i<n; ++i)
{
if (isalpha(buf[i]))
{
p[m]=i;
s[m++]=tolower(buf[i]);
}
}
for (i=0; i<m; ++i)
{
for (j=0; i-j>=0 && i+j<m; ++j)
{
if (s[i-j]!=s[i+j])
{
break;
}
if (j*2+1>max)
{
max=j*2+1;
x=p[i-j];
y=p[i+j];
}
}
for (j=0; i-j>=0 && i+j+1<m; ++j)
{
if (s[i-j]!=s[i+j+1])
{
break;
}
if (j*2+2>max)
{
max=j*2+2;
x=p[i-j];
y=p[i+j+1];
}
}
}
end=clock();
printf("A longest palindrome substring is:\n\n");
for (i=x; i<=y; ++i)
{
printf("%c",buf[i]);
}
printf("\n");
memset(buf,0,sizeof(buf));
}
return 0;
}
输入有点麻烦 展开
A palindrome is a string which reads the same forwards as it does backwards—e.g., gig, radar, and peep. Write a program which inputs a string of at most 1000 letters from the alphabet {a, b, c, . . . , z} and outputs a longest substring which is a palindrome.
Input
The standard input file contains a single string of lowercase letters of length at most 1000. The string might be split onto more than one line, but in that case the individual lines should be taken as a single string, ignoring line separators (a.k.a. carriage returns, new lines).
Output
Output a longest substring of the input which is a palindrome. If there are two or more equally long palindrome substrings, output the leftmost one.
Sample Input
bahuibyasfhbhuahsfbuhuashfbuabcdedcbakasjfkjasjaskjajhuye
Sample Output
A longest palindrome substring is:
abcdedcba
我的代码:
#include<cstdio>
#include<cstring>
#include<cctype>
#include<ctime>
#define MAXN 1000+10
char buf[MAXN], s[MAXN];
int p[MAXN];
int main()
{
int n, m=0, max=0, x, y, i, j;
while(gets(buf))
{
n=strlen(buf);
for (i=0; i<n; ++i)
{
if (isalpha(buf[i]))
{
p[m]=i;
s[m++]=tolower(buf[i]);
}
}
for (i=0; i<m; ++i)
{
for (j=0; i-j>=0 && i+j<m; ++j)
{
if (s[i-j]!=s[i+j])
{
break;
}
if (j*2+1>max)
{
max=j*2+1;
x=p[i-j];
y=p[i+j];
}
}
for (j=0; i-j>=0 && i+j+1<m; ++j)
{
if (s[i-j]!=s[i+j+1])
{
break;
}
if (j*2+2>max)
{
max=j*2+2;
x=p[i-j];
y=p[i+j+1];
}
}
}
end=clock();
printf("A longest palindrome substring is:\n\n");
for (i=x; i<=y; ++i)
{
printf("%c",buf[i]);
}
printf("\n");
memset(buf,0,sizeof(buf));
}
return 0;
}
输入有点麻烦 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询