POJ 2001:shortest Prefixes__pku

Source: Internet
Author: User
Shortest prefixes
Time Limit: 1000MS Memory Limit: 30000K
Total submissions: 16782 accepted: 7286

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 isn't considered a prefix in this problem but every non-empty string are considered to be a PR Efix of itself. In everyday language, we tend to abbreviate words by prefixes. For example, the "carbohydrate" is commonly abbreviated by "carb". In this problem, given a set of words, you'll find for each word the shortest prefix that uniquely identifies the word I T represents.

In the sample input below, "carbohydrate" can is abbreviated to "Carboh", but it cannot is abbreviated to "Carbo" (or Anyt Hing Shorter) because there are other words in the ' list that ' begin with ' Carbo '.

An exact match would override a prefix match. For example, the prefix ' car ' matches the given word ' car ' exactly. Therefore, it is understood without ambiguity the ' car ' is a abbreviation for ' car ', not for ' carriage ' or any of the O ther words in the ' list that begins ' with ' car '.

Input the input contains at least two and but no more than 1000. Each line contains one word consisting of the 1 to lower case letters.

Output the output contains the same number of lines as the input. Each line to the output contains the word from the "corresponding line of" the input, followed by one blank spaces, and the S Hortest prefix that uniquely (without ambiguity) identifies this word.

Sample Input

Carbohydrate
cart
carburetor
Caramel
Caribou
carbonic
cartilage 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


Ask: Give a number of strings, to find the smallest degree of their initials can be formed.

Such as:

Abdef

Abcdefg

Ab

The minimum abbreviation is

Abd

Abc

Ab


You can use the dictionary tree to find the individual words that derive 1 from the edge, and then output.


#include <stdio.h> #include <stdlib.h> #include <string.h> struct tree {char C;
    struct tree *next[27];
int num;
};
    void Init (char *c,tree *t) {tree *p; for (int i=0; i< (int) strlen (c); i++) {if (t->next[c[i]-' a ']==null) {p= (tree*) malloc (
            sizeof (tree));
            t->next[c[i]-' a ']=p;
            p->c=c[i];
            T=t->next[c[i]-' a '];
            memset (t->next,0,sizeof (T->next));
        t->num=1;
            } else {t=t->next[c[i]-' a '];
        t->num++;
    }} void PR (char *c,tree *t) {t=t->next[c[0]-' a '];
            for (int i=0; i< (int) strlen (c); i++) {if (t->num>1) {printf ("%c", t->c);
        T=t->next[c[i+1]-' a '];
            else {printf ("%c", t->c);
        Break
    int main () {Char c[1005][35]={{0}}};
    int i; Tree *t= (tree*) malloc (sizeof (tree));
    t->num=0;
    memset (t->next,0,sizeof (T->next));
    for (i=0; gets (c[i)); i++) {init (c[i],t);
        for (int k=0; k<i; k++) {printf ("%s", C[k]);
        PR (c[k],t);
    printf ("\ n");
return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.