hdu 1004 Let the Balloon Rise

水题,用map做最爽

//法1 直接模拟 0MS

#include <iostream>
using namespace std;
int
main()
{

    char
str[1001][16];
    int
ct[1001];
    int
n,i,j,index; //因为不是每次输入一个颜色都是新的,只有输入新颜色时才需要增加index--颜色的种类数
    while(scanf("%d",&n)!=EOF&&n)
    {

        index=0;
        char
tmp[16];
        memset(ct,0,sizeof(ct)); //ct[]需要重用,所以每次都要初始化
        while(n--)
        {

            scanf("%s",tmp);
            for
(j=0;j<index;j++) {if(strcmp(tmp,str[j])==0) {ct[j]++;break;}} //遍历旧的颜色,如果第j种颜色和当前输入相同,则ct[j]增加1次
            if(j==index) memcpy(str[index++],tmp,sizeof(tmp)); //是新的颜色
        }
        int
mx=INT_MIN,tar; //找出现次数最多的
        for(i=0;i<index;i++)
            if
(ct[i]>mx) {mx=ct[i];tar=i;}
            printf("%s\n",str[tar]);
    }

    return
0;
}

//法2:字典树无聊做法 0Ms

#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

char
ans[16];

typedef struct
trie_node
{

    trie_node()
    {

        count=0;
        int
i;
        for
(i=0;i<26;i++)
            next[i]=NULL;
    }

    int
count;
    trie_node *next[26];
}
trie_node,*trie;

void
trie_insert(trie &root,char *cstr,int &max)
{

    if
(!root) root=new trie_node;
    trie tp=root;
    int
i,t=0;
    char
*str=cstr;
    while
(*str)
    {

        i=*str-'a';
        if
(!tp->next[i]) tp->next[i]=new trie_node;
        tp=tp->next[i];
        str++;
    }

    t=++tp->count;
    if
(t>max)
    {

        max=t;
        strcpy(ans,cstr);
    }
}


void
trie_destroy(trie &p)
{

    if
(!p)return;
    int
i;
    for
(i=0;i<10;i++)
        trie_destroy(p->next[i]);
    free(p);
}


int
main()
{

    int
n;
    char
temp[16];
    while
(scanf("%d",&n)!=EOF && n)
    {

        trie ttree=NULL;
        int
max=0;
        while
(n--)
        {

            scanf("%s",temp);
            trie_insert(ttree,temp,max);
        }

        printf("%s\n",ans);
        trie_destroy(ttree);
    }

    return
0;
}

 

//法3 map 15MS

#include<iostream>
#include<string>
#include<map>

using namespace std;

 
int
main()
{

    char
color[16],index[16];
    map<string,int>col_com;
    int
time,max;
    while
(cin>>time&&time)
    {

        max=0;
        while
(time--)
        {

            cin>>color;
            ++
col_com[color];
            if
(col_com[color]>max)
                {
strcpy(index,color);max=col_com[color];}
        }

        cout<<index<<endl;
        col_com.clear();
    }

    return
0;
}

posted @ 2010-08-18 14:41  菜到不得鸟  阅读(229)  评论(0)    收藏  举报