poj2001 Shortest Prefixes (trie树)
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
题意:给你多个字符串,对每一个字符串你要用最简单但是确定代表这个字符串的前缀表示它。
思路:先把所有的字符串都插入到trie上,这个字符经过的节点val值都加1,那么一个字符的唯一前缀就是从它的头字符一直往后走,当节点val为1时就代表只有这个字符串,那么前缀就是之前的这些数,这里要注意前缀是祺本身的情况要特判。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
const long double eps=1e-13;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 5000
#define maxnode 50000
char s[maxn][30],str[maxn][30];
int val[maxnode];
int ch[maxnode][30];
int sz;
void init()
{
sz=0;
memset(ch[0],0,sizeof(ch[0]));
memset(val,0,sizeof(val));
}
int idx(char s){
return s-'a';
}
void charu(char *s)
{
int u=0,i,j;
int len=strlen(s);
for(i=0;i<len;i++){
int c=idx(s[i]);
if(!ch[u][c]){
sz++;
ch[u][c]=sz;
val[sz]++;
u=sz;
}
else{
u=ch[u][c];
val[u]++;
}
}
}
int chazhao(char *s)
{
int i,j,u=0;
int t,len=strlen(s);
t=len-1;
for(i=0;i<len;i++){
int c=idx(s[i]);
if(val[ch[u][c] ]==1){
t=i;break;
}
u=ch[u][c];
}
return t;
}
int main()
{
int n,m,i,j,tot;
tot=0;
init();
while(scanf("%s",s[++tot])!=EOF){
charu(s[tot]);
}
for(i=1;i<=tot;i++){
int k=chazhao(s[i]);
for(j=0;j<=k;j++){
str[i][j]=s[i][j];
}
str[i][j]='\0';
}
for(i=1;i<=tot;i++){
printf("%s %s\n",s[i],str[i]);
}
}