Poj 290 enhanced animal sorting

Source: Internet
Author: User

Question link: http://acm.nyist.net/JudgeOnline/problem.php? PID = 1, 290

 

Idea: dictionary tree.

 

#include <iostream>#include <malloc.h>#include <cstdio>#include <cstring>using namespace std;struct tirenode{    int num;    struct tirenode * b[26];}tire;  void init(){    for(int i = 0; i < 26; i++)        tire.b[i] = NULL;}int insert(char data[10]){    int i;    int len = strlen(data);        struct tirenode * temp = &tire;        for(i = 0; i < len; i++)    {            if(temp->b[data[i] - ‘a‘] == NULL)        {            temp->b[data[i] - ‘a‘] = (struct tirenode *)malloc( sizeof(struct tirenode) );            for(int j = 0; j < 26; j++)                temp ->b[data[i] - ‘a‘] -> b[j] = NULL;            if(i == len-1)            {                temp ->b[data[i] - ‘a‘] -> num = 1;                return temp ->b[data[i] - ‘a‘] -> num;            }            else            {                temp ->b[data[i] - ‘a‘] -> num = 0;             }        }        else        {            if(i == len-1)            {                temp ->b[data[i] - ‘a‘] -> num++;                return temp ->b[data[i] - ‘a‘] -> num;            }        }        temp = temp->b[data[i] - ‘a‘];    }}int main(){    int n;    scanf("%d", &n);    char c[10];    int max = 0;    char res[10];    int num = 0;        init();        for(int i = 0; i < n; i++)    {        scanf("%s",c);        num = insert(c);        if( max < num )        {            max = num;            strcpy(res,c);        }    }        printf("%s %d\n",res,max);     return 0;} 

 

Poj 290 enhanced animal sorting

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.