zoukankan      html  css  js  c++  java
  • poj 2001

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    using namespace std;
    struct trie {
    trie *a[27];
    int v;
    }*h,*t;
    int len,k;
    char str[1100][30];
    void build(int count,trie *h) {
    int i,j;
    h->v++;
    if(count>len)return ;
    if(h->a[str[k][count]-'a']!=NULL)build(count+1,h->a[str[k][count]-'a']);
       else {//必须用else  否则他执行完上面的if后还会执行下面的for语句
      for(i=count;i<=len;i++) {
     t=new trie;
     t->v=1;
     for(j=0;j<26;j++)t->a[j]=NULL;
     h->a[str[k][i]-'a']=t;
     h=t;
      }
    }
      return ;
    }
    void find(int count ,int m,trie *h) {
    printf("%c",str[m][count]);
    if(count==strlen(str[m])-1||h->v==1)return ;
    find(count+1,m,h->a[str[m][count+1]-'a']);
    }
    int main() {
    int i;
          h=new trie;h->v=0;
     for(i=0;i<26;i++)
     h->a[i]=NULL;
     k=0;
     while(scanf("%s",str[++k])!=EOF) {
     len=strlen(str[k])-1;
     build(0,h);
     }
     k--;
        for(i=1;i<=k;i++) {
    printf("%s ",str[i]);
    find(0,i,h->a[str[i][0]-'a']);
    if(i!=k)
    printf(" ");
    }
    return 0;

    }

    //感觉下面这个比上面那个好理解

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    using namespace std;
    struct node {
    int num;
    node *a[27];
        node () {
    int i;
    num=0;
    for(i=0;i<26;i++)
    a[i]=NULL;
    }
    }*root;
    char str[1100][30];
    int k;
    void insert(node * root,int len) {
    root->num++;
    struct node *q;
    int i;
    for(i=0;i<=len;i++) {
    if(root->a[str[k][i]-'a']!=NULL) {
       root=root->a[str[k][i]-'a'];
    root->num++;
    }
    else {
    q=new node ();
    q->num++;
    root->a[str[k][i]-'a']=q;
    root=q;
    }
    }
    return ;
    }
    void find(int len,int m,node *root) {
          int i;  
     for(i=0;i<=len;i++) {
     root=root->a[str[m][i]-'a'];
     if(root->num==1) {
    printf("%c",str[m][i]);
    return ;
     }
     else 
               printf("%c",str[m][i]);
     }
     return ;
    }
    int main(){
    int i,len;
        root=new node ();
    k=0;
    while(scanf("%s",str[++k])!=EOF) {
    len=strlen(str[k])-1;
    insert(root,len);
    }
    k--;
    for(i=1;i<=k;i++) {
    printf("%s ",str[i]);
    find(strlen(str[i])-1,i,root);
    if(i!=k)
    printf(" ");
    }
    return 0;
    }

  • 相关阅读:
    迭代器
    逻辑量词
    How to distinguish between strings in heap or literals?
    Is the “*apply” family really not vectorized?
    power of the test
    The Most Simple Introduction to Hypothesis Testing
    析构函数允许重载吗?
    C++中析构函数的作用
    在C#中的构造函数和解析函数
    c++构造函数与析构函数
  • 原文地址:https://www.cnblogs.com/thefirstfeeling/p/4410920.html
Copyright © 2011-2022 走看看