题目:
Authors of the new, all-in-one encyclopedia have organized the titles in the order they consider most appropriate for their readers. It's not always alphabetical, because they want to observe some peculiar relationships between them. However, they still want to allow users to look up titles quickly.
They achieve this by adding a carefully calculated number of spaces before every title in the list of titles. They call this structure a dictionary.
A dictionary is represented by a list of words with some number of spaces before certain words. Dictionary format can be described as a set of constraints on sequences of consecutive words starting with the same letter. Any maximal sequence of consecutive words starting with the same letter should satisfy the following rules:
The authors don't feel like giving you a more detailed explanation of what a dictionary is, so they have included an example (see sample input and output) that clarifies their definition.
Your task is to write a program that will convert a given list of words into a dictionary by adding some number of spaces before certain words and preserving the original order of the words.
Sample Input
They achieve this by adding a carefully calculated number of spaces before every title in the list of titles. They call this structure a dictionary.
A dictionary is represented by a list of words with some number of spaces before certain words. Dictionary format can be described as a set of constraints on sequences of consecutive words starting with the same letter. Any maximal sequence of consecutive words starting with the same letter should satisfy the following rules:
- The first word in the group has no spaces before it. Every subsequent word in the group has at least one leading space.
- If
- the first word of the group is deleted and
- one space is deleted before every remaining word and
- the first letter is deleted from every remaining word
then resulting sequence is a dictionary. - the first word of the group is deleted and
The authors don't feel like giving you a more detailed explanation of what a dictionary is, so they have included an example (see sample input and output) that clarifies their definition.
Your task is to write a program that will convert a given list of words into a dictionary by adding some number of spaces before certain words and preserving the original order of the words.
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 bornSample 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
题意:
Description
给出一些单词,按规律输出,规律是让后面一个字符串和前面一个字符串比较,如果前几位相同的字符数量大于空格的数量,那么空格的数量加1,如果前几位相同字符数量小于空格的数量,那么空格数量变为相同字符的数量
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
给出一些单词,按规律输出,规律是让后面一个字符串和前面一个字符串比较,如果前几位相同的字符数量大于空格的数量,那么空格的数量加1,如果前几位相同字符数量小于空格的数量,那么空格数量变为相同字符的数量
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
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
思路:这题是
字符串的处理,主要在于我们怎么求出每个单词前的空格数count,根据题意易得当相邻两串公共前缀大于count时,count++,否则count=公共前缀长度,以此类推即可 。我们可以比较每个字符串与前面的字符串相同的字符的个数,如果相同的字符的个数比上一字符串与上上一个字符串相同的字符多,那么就让此字符串前面,在上一字符串已有的空格的基础上多增加一个空格,相反如果是小于,具体ac代码如下,比较容易理解:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
char a[100] , b[100] ;
int count = 0 ;
while(gets(b)){
int i = 0 , j = 0 ;
while(i < strlen(a) && j < strlen(b)){
if(a[i] == b[j]){
i ++ ;
j ++ ;
}
else
break;
}
if(i <= count){
count = i ;
}
if(i > count){
count += 1 ;
}
strcpy(a,b);
for(int k = 0 ; k < count ; k ++){
printf(" ");
}
puts(b);
}
return 0 ;
}