HDU-1004-提供四种解法--map解法/自己写二叉排序树/hash/字典树

Let the Balloon Rise

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 107610    Accepted Submission(s): 41752


Problem Description
Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.

This year, they decide to leave this lovely job to you.
 

Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) -- the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.

A test case with N = 0 terminates the input and this test case is not to be processed.
 

Output
For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.
 

Sample Input
  
  
5 green red blue red red 3 pink orange pink 0
 

Sample Output
  
  
red pink
 

Author
WU, Jiazhi
 

Source
//map函数的应用
#include<iostream>
#include<string>
#include<map>
using namespace std;
map<string, int>vis;
int main()
{
    int n;
    int max = -1;
    string t;
    string s;
   
    scanf("%d", &n);
    while (n != 0)
    {
        vis.clear();
        max = -1;
        while (n--)
        {
            cin >> s;
            vis[s]++;
            if (vis[s]>max)
            {
                max = vis[s];
                t = s;
            }

        }
        cout <<t<< endl;
        cin >> n;
    }
    return 0;
}

//二叉排序树
#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
string s;
int Max = -1;
struct node
{

 node() {}
 node(string A) { this->a = A;b = 1; lchild = NULL;rchild = NULL; }
 string a;
 int b;
 node *lchild;
 node *rchild;

};
void inorder(node *T)
{
 if (T == NULL) return;
 if ((T->b>Max) || (T->b == Max&&T->a<s))
 {
  Max = T->b;
  s = T->a;

 }
 inorder(T->lchild);
 inorder(T->rchild);

}
void  insert(node *&T, string c)
{
 if (T == NULL)
 {
  T = new node(c);
  return;
 }
 if (T->a<c)
 {
  insert(T->rchild, c);
 }
 else if (T->a>c)
 {
  insert(T->lchild, c);
 }
 else
 {
  T->b++;

 }


}


int main()
{
 int n;
 string p;
 cin >> n;
 while (n != 0)
 {
  Max = -1;
  cin >> p;
  s = p;
  node *T = new node(p);
  --n;
  while (n--)
  {
   cin >> p;
   insert(T, p);

  }
  inorder(T);
  cout << s << endl;
  cin >> n;


 }

 return 0;
}


//边建立节点边操作
#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
string s;
int Max = -1;
struct node
{

 node() {}
 node(string A) { this->a = A;b = 1; lchild = NULL;rchild = NULL; }
 string a;
 int b;
 node *lchild;
 node *rchild;

};

void  insert(node *&T, string c)//插入操作
{
 if (T == NULL)
 {
  T = new node(c);
  return;
 }
 if (T->a<c)
 {
  insert(T->rchild, c);
 }
 else if (T->a>c)
 {
  insert(T->lchild, c);
 }
 else
 {
  T->b++;
  if ((Max < T->b)||(Max==T->b)&&T->a<s)
  {
   Max = T->b;
   s = T->a;
  }
  
 }

}

int main()
{
 int n;
 string p;
 cin >> n;
 while (n != 0)
 {
  Max = -1;
  cin >> p;
  s = p;
  node *T = new node(p);
  --n;
  while (n--)
  {
   cin >> p;
   insert(T, p);

  }
  cout << s << endl;
  cin >> n;


 }

 return 0;
}

//散列表函数的应用
#include <bits/stdc++.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
int c[10000];
string name[10000];
unsigned int hashf(const char *str)
{
    register unsigned int h;
    register unsigned char *p;

    for(h=0, p = (unsigned char *)str; *p ; p++)
        h = 31 * h + *p;

    return h%10000;
}
main ()
{
    int n;
    string s;
    while(cin>>n)
    {
        memset((void *)c,0,sizeof(c));
        while(n--)
        {
            cin>>s;
            if(name[hashf(s.c_str())]=="")
                name[hashf(s.c_str())]=s;
            c[hashf(s.c_str())]++;
        }

        int maxi=0;
        for(int i=0; i<sizeof(c)/sizeof(int); i++)
        {
            if(c[maxi]<c[i]) maxi=i;
        }

        cout<<name[maxi]<<endl;


    }

    return 0;
}

#include<stdio.h>
#include<string.h>
struct node
{
int cnt;
node *next[26];
node()
{
cnt=0;
for(int i=0;i<26;i++)
next[i]=NULL;
}
}*trie,p;
int cnt;
char res[100];
int max;
void insert(char word[])
{
trie=&p;
for(int i=0;word[i]!='\0';i++)
{
int t=word[i]-'a';
if(trie->next[t]==NULL)
{
trie->next[t]=new node;
}
trie=trie->next[t];
}
trie->cnt++;
if(trie->cnt>max)
{
max=trie->cnt;
strcpy(res,word);
}
}
void init()
{
trie=&p;
for(int i=0;i<26;i++)
{
trie->next[i]=NULL;
trie->cnt=0;
}
}
int main()
{
int n;
char word[20];
while(scanf("%d",&n),n)
{
memset(word,0,sizeof(word));
cnt=0;
max=0;
getchar();
for(int i=0;i<n;i++)
{
scanf("%s",word);
insert(word);
}
printf("%s\n",res);
}
return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值