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;
}
输入有点麻烦
展开
 我来答
starfar1983
2010-12-22 · TA获得超过2258个赞
知道小有建树答主
回答量:692
采纳率:0%
帮助的人:1036万
展开全部
先把所有输入都读完,再去做进一步处理。

读取代码:差做
char *pBuf;
memset(buf, 0, sizeof(buf); // buf是你前面定义的数组
pBuf = buf;
while(gets(pBuf)) pBuf += strlen(pBuf);

这样,所有字符都被读进了buf,并且所有换行符都被去除了。接虚搭衡下来你只要实现枝咐算法部分就可以了。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式