字典树:动物统计加强版 (nyoj)

来源:互联网 发布:机场 知乎 编辑:程序博客网 时间:2024/06/05 05:34
 #include<bits/stdc++.h>using namespace std;struct node{ struct    node *next[26];    int cot;} a[1000000];node *now=a;int insert(node  * &root,char *str){    if(!root)        root=now++;    node  *p=root;    while(*str)    {  //printf("sssss");        int i=*str-'a';        if(!p->next[i])            p->next[i]=now++;        p=p->next[i];        str++;    }    p->cot++;    return p->cot;}int main(){memset(a, 0, sizeof(node) * 1000000);    int n;    scanf("%d",&n);    char str[11];    char t[11];    int ans=0;    node *root;    root=NULL;    while(n--)    {        scanf("%s",str);        int temp=insert(root,str);        if(temp>ans)        {            strcpy(t,str);            ans=temp;        }    }    printf("%s %d\n",t,ans);    return 0;}        

0 0
原创粉丝点击