poj 1750

Dictionary
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2123 Accepted: 647

Description

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.

Input

The input consists of at least one and most 100000 words. Each word consists of at least one and at most 10 lower-case letters. There will be no leading or trailing spaces. There will be no blank lines between the words, but there may be an arbitrary number of blank lines at the end of the file.

Output

Write to the output the original words in the same order without any trailing spaces but with the appropriate number of leading spaces, so that this word list is a dictionary. There should be no blank lines between the words, but there may be an arbitrary number of blank lines at the end of the file.

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
题意:相同的字符向后面措一位空格……
题解:查找相同的字符个数,输出多少个空格……
代码:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int maxn=100000;
int check(char *a,char *b)
{
	int lena=strlen(a);
	int lenb=strlen(b);
	int i=0;
	while(i<lena&&i<lenb)
	{
		if(a[i]!=b[i])
		 break;
		 else
		 i++;
	}	
	return i;
	
}
int main()
{
	char str[2][11];
	int change=1;
	
	
cin>>str[0];
	cout<<str[0]<<endl;
	int spacenum;
	int thesamenum=0;
    while(scanf("%s", str[change]) != EOF)
    {
    	thesamenum=check(str[change],str[!change]);
    	if(thesamenum<=spacenum)
    	 spacenum=thesamenum;
    	 else
    	  spacenum++;
    	 
       printf("%*s\n",spacenum+strlen(str[change]), str[change]);
    /*

	for(int i=0;i<spacenum;i++)
    	{
	    	putchar(' ');
	    }
	    cout<<str[change]<<endl;*/
    	change=!change;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值