POJ2001解题报告

Shortest Prefixes
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 6143   Accepted: 2512

Description

A prefix of a string is a substring starting at the beginning of the given string. The prefixes of "carbon" are: "c", "ca", "car", "carb", "carbo", and "carbon". Note that the empty string is not considered a prefix in this problem, but every non-empty string is considered to be a prefix of itself. In everyday language, we tend to abbreviate words by prefixes. For example, "carbohydrate" is commonly abbreviated by "carb". In this problem, given a set of words, you will find for each word the shortest prefix that uniquely identifies the word it represents.

In the sample input below, "carbohydrate" can be abbreviated to "carboh", but it cannot be abbreviated to "carbo" (or anything shorter) because there are other words in the list that begin with "carbo".

An exact match will override a prefix match. For example, the prefix "car" matches the given word "car" exactly. Therefore, it is understood without ambiguity that "car" is an abbreviation for "car" , not for "carriage" or any of the other words in the list that begins with "car".

Input

The input contains at least two, but no more than 1000 lines. Each line contains one word consisting of 1 to 20 lower case letters.

Output

The output contains the same number of lines as the input. Each line of the output contains the word from the corresponding line of the input, followed by one blank space, and the shortest prefix that uniquely (without ambiguity) identifies this word.

Sample Input

carbohydrate
cart
carburetor
caramel
caribou
carbonic
cartilage
carbon
carriage
carton
car
carbonate

Sample Output

carbohydrate carboh
cart cart
carburetor carbu
caramel cara
caribou cari
carbonic carboni
cartilage carti
carbon carbon
carriage carr
carton carto
car car
carbonate carbona
思路:对每个字符串快速排序,然后对相邻的两个串比较确定唯一前缀。。。
#include<iostream>
using namespace std;

char *a[2000],*b;	int n,list[1500],c;

void q(int left,int right)
{
	if(left>=right)	return ;
	int i=left,j=right;
	while(i!=j)
	{
		for(;strcmp(a[j],a[i])>0&&i!=j;j--);
		if(i!=j){	b=a[i];		a[i]=a[j];	a[j]=b;		c=list[i];	list[i]=list[j];	list[j]=c;	i++;}
		for(;strcmp(a[i],a[j])<0&&i!=j;i++);
		if(i!=j){	b=a[i];		a[i]=a[j];	a[j]=b;		c=list[i];	list[i]=list[j];	list[j]=c;	j--;}		
	}
	q(left,i-1);
	q(i+1,right);
}

int main()
{
	char temp[25];	int i,j;
	n=0;
	for(i=0;i<1500;i++)	list[i]=i;
	while((a[n]=new char[40])&&scanf("%s",a[n])!=EOF)
		n++;
	q(0,n-1);
	for(i=0;i<n;i++)
		for(j=0;j<n;j++)
			if(list[j]==i)
			{
				int q=0,z1=0,z2=0,h=0,z;
				if(j>0)
				{
					while(q<strlen(a[j-1])&&z1<strlen(a[j])&&a[j-1][q]==a[j][z1])
					{	q++;	z1++;};
				}
				if(j<n-1)
				{
					while(h<strlen(a[j+1])&&z2<strlen(a[j])&&a[j+1][h]==a[j][z2])
					{	h++;	z2++;};
				}
				z=z2;
				if(z1>z2)	z=z1;
				cout<<a[j]<<' ';
				for(q=0;q<=z&&a[j][q]!='/0';q++)	cout<<a[j][q];
				cout<<endl;
			}
	return 0;
}
 

你可能感兴趣的:(list,String,ini,input,each,output)