hdu 1800 Flying to the Mars (字典树)

来源:互联网 发布:淘宝头条写手招募 编辑:程序博客网 时间:2024/05/16 10:03


1.死循环会给出The limit exceeded,while(scanf("%d",n))别忘了写!=EOF。

2.一种字符串加数字的操作,加几会把前面的字符消除几位。

#include<stdio.h>#include<string.h>#include<stdlib.h>typedef struct node{    struct node*next[10];    bool exist;    int count;}*trie,node;trie root;int m;void creat(trie &p){    p=(trie)malloc(sizeof(node));    for(int i=0;i<10;i++)        p->next[i]=0;    p->exist=false;    p->count=0;}void insert(trie p,char s[]){    int k=0 ;    while(s[k]!='\0'){        if(!p->next[s[k]-'0']){            trie q;            creat(q);            p->next[s[k]-'0']=q;        }        p=p->next[s[k]-'0'];        k++;    }    p->exist=true;    p->count++;    if(p->count>m)        m=p->count;}int main(){    int n,i;    char s[31];    while(scanf("%d",&n)!=EOF){           creat(root);        m=0;        for(i=0;i<n;i++){            scanf("%s",s);            int j=0;            while(s[j]=='0'){  /*又学会一种姿势*/                j++;            }            insert(root,s+j);        }        printf("%d\n",m);    }    return 0;}