# POJ 1750 Dictionary（水~）

487人阅读 评论(0)

Description

Input

Output

Sample Input
a
ant
antique
amaze
bargain
bridge
bride
bribe
born
bucket
tart
tan
tram
trolley
t
try
trial
zed
double
dorm
do
dormant
donate
again
agony
boost
back
born
Sample Output
a
ant
antique
amaze
bargain
bridge
bride
bribe
born
bucket
tart
tan
tram
trolley
t
try
trial
zed
double
dorm
do
dormant
donate
again
agony
boost
back
born
Solution

Code

#include<stdio.h>
#include<string.h>
char str[100001][12];
int cmp(char a[],char b[])//计算两个串的公共前缀长度
{
int i,len1,len2,count=0;
len1=strlen(a);
len2=strlen(b);
for(i=0;i<len1&&i<len2;i++)
if(a[i]==b[i])
count++;
else
break;
return count;
}
int main()
{
int i,j,k,count;
fscanf(stdin,"%s",str[0]);
printf("%s\n",str[0]);//第一个单词直接输出即可
count=0;//初始空格数为0
i=1;
while(fscanf(stdin,"%s",str[i])!=EOF)
{
k=cmp(str[i],str[i-1]);
if(k>count)//若两串公共前缀长度大于count则count++
count++;
else//否则令count=k
count=k;
for(j=0;j<count;j++)//输出count个空格
printf(" ");
printf("%s\n",str[i]);//输出单词
i++;
}
return 0;
}

个人资料
等级：
访问量： 57万+
积分： 2万+
排名： 307
最新评论