POJ2001 Shortest Prefixes

描述

传送门:我是传送门

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”.

输入

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.

输出

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.

样例

输入

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

输出

carbohydrate carboh
cart cart
carburetor carbu
caramel cara
caribou cari
carbonic carboni
cartilage carti
carbon carbon
carriage carr
carton carto
car car
carbonate carbona

思路

题目大意

是在输出原字符串的同时找出原字符串的唯一最短前缀

$(唯一:这个前缀只在这个单词出现过)$
如果没找到的话则输出原字符串

类似 HDU1251 统计难题 略微升级版

使用 $ num[N] $ 数组保存当前前缀出现次数

$num[i]$ 代表以当前字符串为前缀的单词数量

若 $num[i] > 1$ 则说明当前前缀不止出现过一次,也就是说还需要往后找才可以找到唯一的前缀

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* =========================================================================
*
* Filename: poj2001.cpp
*
* Link:
*
* Version: 1.0
* Created: 2018/08/29 14时09分22秒
* Revision: none
* Compiler: g++
*
* Author: 杜宁元 (https://duny31030.top/), duny31030@126.com
* Organization: QLU_浪在ACM
*
* =========================================================================
*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
#define clr(a, x) memset(a, x, sizeof(a))
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define pre(i,a,n) for(int i=a;i>=n;i--)
#define ll long long
#define max3(a,b,c) fmax(a,fmax(b,c))
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int N = 5e5+10;
struct node
{
int next[27];
}trie[N];
int num[N];
char s[N][30];
int tot = 0;
int size = 0;
void add(char a[])
{
int i,now = 0;
int len = strlen(a);
for(i = 0;i < len;i++)
{
int tmp = a[i]-'a'+1;
int next = trie[now].next[tmp];
if(next == 0)
trie[now].next[tmp] = ++tot;
now = trie[now].next[tmp];
num[now]++;
}
}
void query(char a[])
{
int len = strlen(a);
int now = 0,i;
for(i = 0;i < len;i++)
{
if(num[now] == 1)
break;
int tmp = a[i]-'a'+1;
printf("%c",a[i]);
now = trie[now].next[tmp];
}
}

int main()
{
ios
#ifdef ONLINE_JUDGE
#else
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif
while(scanf("%s",s[size++]) != EOF)
add(s[size-1]);
rep(i,0,size-1)
{
printf("%s ",s[i]);
query(s[i]);
printf("\n");
}
fclose(stdin);
// fclose(stdout);
return 0;
}