2001 -- Shortest Prefixes
找能识别出每个单词所需的最少的长度(非公共前缀),并打印。如果这个最少的长度是它本身就打印本身。
http://blog.csdn.net/lsjseu/article/details/8852440
Read full article from 2001 -- 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".
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
找能识别出每个单词所需的最少的长度(非公共前缀),并打印。如果这个最少的长度是它本身就打印本身。
http://blog.csdn.net/lsjseu/article/details/8852440
trie建树,设置一个变量num,计算每个单词出现的次数。查找的时候如果num = 1则说明这是第一次出现过的单词,直接返回。
在建tire的时候,设置一个num变量记录孩子节点的个数,如果孩子节点为1表示是一个单词节点,如果num>1表示是一个中间节点,继续搜索。
采用采用链表的方式建Trie树- char str[1005][25];
- struct Trinode{
- int num;//记录孩子节点的个数
- Trinode *link[26];
- Trinode(int _num=0):num(_num)
- {
- for(int i=0;i<26;i++){
- link[i] = NULL;
- }
- }
- };
- class Trie{
- public:
- Trie()
- {
- root = new Trinode(0);
- }
- void insert(char *tar)
- {
- char ch;
- Trinode *ptr = root;
- while(*tar){
- ch = *tar-'a';
- if(ptr->link[ch]==NULL){
- ptr->link[ch] = new Trinode(1);//单词节点的num为1
- }
- else
- ptr->link[ch]->num++;
- ptr = ptr->link[ch];
- tar++;
- }
- }
- void seach(char *tar)
- {
- int i = 0;
- char ch;
- Trinode* ptr = root;
- while(ch = *tar){
- cout<<ch;
- if(ptr->link[ch-'a']->num == 1)return;
- ptr = ptr->link[ch-'a'];
- tar++;
- }
- }
- private:
- Trinode *root;
- };
- class ShortestPrefixes{
- public:
- void solution()
- {
- int n,i;
- Trie tree;
- while(scanf("%s",str[n])!=EOF)
- //cin>>n;
- //for(int i=0;i<n;i++)
- {
- //scanf("%s",str[i]);
- tree.insert(str[n]);
- n++;
- }
- for(i=0;i<n;++i)
- {
- printf("%s ",str[i]);
- tree.seach(str[i]);
- printf("\n");
- }
- }
- };
- public class Main {
- public static void main(String[] args) {
- Scanner scn = new Scanner(System.in);
- Trie trie = new Trie();
- Queue<String> queue = new LinkedList<String>();
- while (scn.hasNext()) {
- String word = scn.nextLine();
- queue.offer(word);
- trie.insert(word);
- }
- while (!queue.isEmpty()) {
- String word = queue.poll();
- System.out.println(word + " " + trie.search(word));
- }
- scn.close();
- }
- }
- class Trie {
- private Node root;
- public Trie() {
- root = new Node(new Node[26], 0);
- }
- public void insert(String word) {
- Node current = root;
- for (int i = 0; i < word.length(); ++i) {
- if (null != current.getChildrenItem(word.charAt(i) - 'a')) {
- current = current.getChildrenItem(word.charAt(i) - 'a');
- current.setCount(current.getCount() + 1);
- } else {
- Node newNode = new Node(new Node[26], 1);
- current.setChildrenItem(word.charAt(i) - 'a', newNode);
- current = newNode;
- }
- }
- }
- public String search(String word) {
- Node current = root;
- StringBuilder prefix = new StringBuilder();
- for (int i = 0; i < word.length(); ++i) {
- if (1 == current.getCount()) {
- break;
- }
- prefix.append(word.charAt(i));
- current = current.getChildrenItem(word.charAt(i) - 'a');
- }
- if (1 == current.getCount()) {
- return prefix.toString();
- } else {
- return word;
- }
- }
- }
- class Node {
- private Node[] children;
- private int count;
- public Node(Node[] children, int count) {
- this.children = children;
- this.count = count;
- }
- public Node getChildrenItem(int i) {
- return children[i];
- }
- public void setChildrenItem(int i, Node node) {
- children[i] = node;
- }
- public int getCount() {
- return count;
- }
- public void setCount(int count) {
- this.count = count;
- }
- }
Read full article from 2001 -- Shortest Prefixes