Dictionary POJ - 1750

题目:
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 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 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
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

题意:
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

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

思路:这题是 字符串的处理,主要在于我们怎么求出每个单词前的空格数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 ;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值