Shortest Prefixes
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 14619 | Accepted: 6310 |
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".
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
Source
题目大意:给你很多个字符串,让你找每个字符串的最短前缀,但这么多字符串前缀不能有相同的,要能唯一标识一个字符串,而且前缀长度尽量小。
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
struct node
{
int x;
node *next[26];
node()
{
x=0;
memset(next,0,sizeof(next));
}
};
node *root=NULL;
void insert(char *s)
{
node *p=root;
node *p1=NULL;
int i,k;
for(i=0;s[i];i++)
{
k=s[i]-'a';
if(p->next[k]==NULL)
{
p1=new node;
p->next[k]=p1;
}
else
p->next[k]->x++;
p=p->next[k];
}
}
void search(char *s)
{
node *p=root;
int i,k;
for(i=0;s[i];i++)
{
k=s[i]-'a';
if(p->next[k]==NULL)
return ;
p=p->next[k];
printf("%c",s[i]);
if(p->x==0)
break;
}
}
char str[1010][25];
int main()
{
int i=0;
root=new node;
while(~scanf("%s",str[i]))
{
insert(str[i]);
i++;
}
for(int j=0;j<i;j++)
{
printf("%s ",str[j]);
search(str[j]);
printf("\n");
}
return 0;
}
人家的代码
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- const int MAX=26;
- char str[1000][21],res[1000][21];
- struct node{
- struct node *next[MAX]; //字母分支
- int flag; //字母出线过的次数统计
- }node;
- struct node *root; //根节点
- void inset_tree(char *str) //插入字符串
- {
- int ans,i,len;
- struct node *p,*q;
- p=root; //p先指向根节点
- len=strlen(str);
- for(i=0;i<len;i++){
- ans=str[i]-'a'; //每一个字母的字母编号
- if(p->next[ans]!=NULL) //如果非空
- {
- p=p->next[ans]; //p指向该字母节点
- p->flag++; //该字母出现过的次数加1
- }
- else{
- q=(struct node*)calloc(1,sizeof(node)); //创建新节点
- p->next[ans]=q; //q赋给该节点
- p=q; //p指向该节点
- p->flag=1; //节点数为1
- }
- }
- }
- void find(char *str,char *res) //查找
- {
- int len,ans,i;
- struct node *p;
- p=root;
- len=strlen(str);
- for(i=0;i<len;i++){
- ans=str[i]-'a';
- p=p->next[ans]; //p指向该字母节点
- res[i]=str[i]; //该字母赋给结果数组
- if(p->flag==1) //如果该字母是这个字符串所独有的
- {
- res[++i]='\0'; //查找完成
- return ;
- }
- }
- }
- int main(){
- int n=0,i;
- root=(struct node *)calloc(1,sizeof(node));
- while(scanf("%s",str[n])!=EOF) //插入输入的每一个字符串
- {
- inset_tree(str[n]);
- n++;
- }
- for(i=0;i<n;i++) //查找每一个字符串的最短有效前缀
- find(str[i],res[i]);
- for(i=0;i<n;i++)
- printf("%s %s\n",str[i],res[i]);
- return 0;
- }