Time Limit: 1000MS | Memory Limit: 30000KB | 64bit IO Format: %I64d & %I64u |
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
题意:求出给定的多个字符串各自能只能代表自己的前缀。
思路:刚刚起步做字典树,这一题想了一两天,结果发现是一道很容易的字典树题。先走一遍字典树把字符串记录起来,并记录被多次走过的节点,然后每个字符串再跑一遍字典树,记录最后一个被重复走过的节点的位置ans,答案就是字符串0到ans+1(如果ans+1位置上还有字母)字符串。
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; char s[1010][25]; int sz; struct Trie { int ch[20010][26]; bool again[20010][26]; Trie() { memset(ch, 0, sizeof(ch)); memset(again, 0, sizeof(again)); sz = 1; } int idx(char c) { return c - 'a'; } void insertt(char *s, int n) { int u = 0; int len = strlen(s); for(int i = 0; i < len; i++) { int c = idx(s[i]); if(!ch[u][c]) { ch[u][c] = sz++; } else { again[u][c] = true; } u = ch[u][c]; } } void find_prefix(char *s) { int len = strlen(s), ans = -1; int u = 0; for(int i = 0; i < len; i++) { int c = idx(s[i]); if(again[u][c]) { ans = i; } u = ch[u][c]; } printf("%s ", s); if(ans + 1 < len) ans++; for(int i = 0; i <= ans; i++) { printf("%c", s[i]); } printf("\n"); } }test; int main() { int n = 1; while(~scanf(" %s", s[n])) { n++; } for(int i = 1; i < n; i++) { test.insertt(s[i], i); } for(int i = 1; i < n; i++) { test.find_prefix(s[i]); } }