zoukankan      html  css  js  c++  java
  • poj 2001, 乱搞

    A - Shortest Prefixes
    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".

    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
    
    #include<iostream>
    #include<string>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    
    struct WORD{
        string str;
        unsigned int len;
    }cas[1003];
    
    int main(){
        unsigned int i,j,n=0,ki,kj;
        while(cin>>cas[n++].str) /*if(n==12)break;*/;
        for(i=0;i<n;i++) cas[i].len=0;
        for(i=0;i<n;i++){
            for(j=i+1;j<n;j++){
                ki=kj=0;
                while(cas[i].str[ki]==cas[j].str[kj]){
                    ki++;kj++;
                    if(ki==cas[i].str.size() ||
                       kj==cas[j].str.size()){
                            if(cas[i].str.size()>cas[j].str.size()){
                                //cas[j].len=cas[j].str.size();
                                ki++;
                            }
                            else{
                                //cas[i].len=cas[i].str.size();
                                kj++;
                            }
                            break;
                       }
                }
                if(!(ki==cas[i].str.size() ||
                       kj==cas[j].str.size())) ki=++kj;
                if(cas[i].len<ki) cas[i].len=ki;
                if(cas[j].len<kj) cas[j].len=kj;
            }
        }
        //cout<<"****************"<<endl;
        for(i=0;i<n;i++){
            cout<<cas[i].str<<' ';
            for(j=0;j<cas[i].len;j++) cout<<cas[i].str[j];
            cout<<endl;
        }
        /*int a;
        while(cin>>a);*/
        return 0;
    }
    /*
    abc
    ab
    a
    */
    
  • 相关阅读:
    用bower命令创建项目
    HBuilder打包ios应用
    响应式布局--引入外部样式
    手机中点击链接或button按钮出现黄色边框的解决办法
    通过输入卡号前10位数字判断是哪个银行的卡和类型(储蓄卡or信用卡)
    只允许输入数字和小数点
    python中的实例方法、静态方法、类方法、类变量和实例变量浅析
    python中的实例方法、静态方法、类方法、类变量和实例变量浅析
    python导入csv文件出现SyntaxError问题分析
    python导入csv文件出现SyntaxError问题分析
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3413039.html
Copyright © 2011-2022 走看看