博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Let the Balloon Rise HDU - 1004 (字典树和map两种写法)
阅读量:4557 次
发布时间:2019-06-08

本文共 1978 字,大约阅读时间需要 6 分钟。

Let the Balloon Rise HDU - 1004

题目链接:

题目:

题目描述
有多种颜色的气球,统计它们的个数,并找出数量最多的那种颜色。
Input

有多组样例输入。每组样例第一行输入一个整数N (0 < N <= 1000) -- 代表一共有N个气球。接下来N行每行输入一个不多于15个字母的字符串代表颜色。

N=0代表输入结束。

Output

每组样例数据输出数量最多的那种颜色的气球。(保证输出唯一)

Sample Input
5greenredblueredred3pinkorangepink0
Sample Output
redpink 思路:比较简单的是map写法,记录字符串对应的个数,而且还是自动排序的:
//// Created by hanyu on 2019/4/9.//#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef long long LL;using namespace std;const int MAX=1e5+7;int main(){ int n; while(cin>>n&&n) { map
sh; string s; for(int i=0;i
>s; sh[s]++; } map
::iterator it; int sum=0; for(it=sh.begin();it!=sh.end();it++) { if(it->second>sum) sum=it->second; } for(it=sh.begin();it!=sh.end();it++) { if(it->second==sum) { cout<
first<

另一种写法是我今天写的字典树的写法,也比较方便

 

// // Created by HJYL on 2019/8/19.//#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;typedef long long ll;const int maxn=1e6+10;struct trie{ trie* next[26]; int sum; trie(){ for(int i=0;i<26;i++) next[i]=NULL; sum=0; }}root;void insert(char* s){ trie* p=&root; for(int i=0;s[i];i++) { if(p->next[s[i]-'a']==NULL) { p->next[s[i]-'a']=new trie; // p->sum=1; //p=p->next[s[i]-'a']; } p=p->next[s[i]-'a']; p->sum++; }}int find(char* s){ trie* p=&root; for(int i=0;s[i];i++) { if(p->next[s[i]-'a']==NULL) return 0; else p=p->next[s[i]-'a']; } return p->sum;}int main(){ //freopen("C:\\Users\\asus567767\\CLionProjects\\untitled\\text","r",stdin); int n,m; char str[maxn][20]; while(~scanf("%d",&n)&&n) { for(int i=0;i
sum) { sum=a; pos=i; } } printf("%s\n",str[pos]); } return 0;}

 

 

转载于:https://www.cnblogs.com/Vampire6/p/11380738.html

你可能感兴趣的文章
【灵异短篇】这个夜晚有点凉
查看>>
一点小问题
查看>>
pytest 10 skip跳过测试用例
查看>>
MVC身份验证及权限管理
查看>>
It was not possible to find any compatible framework version
查看>>
关于8.0.15版本的mysql下载与安装
查看>>
Redis主从复制看这篇就够了
查看>>
洛谷 P1202 [USACO1.1]黑色星期五Friday the Thirteenth 题解
查看>>
(4.20)SQL Server数据库启动过程,以及启动不起来的各种问题的分析及解决技巧...
查看>>
基本数据类型(数字和字符串)
查看>>
函数__装饰器
查看>>
linux system函数分析
查看>>
前端优化措施
查看>>
论学习汉语和学习编程的异同点
查看>>
linux img文件压缩及解压
查看>>
Linux 下的 scp
查看>>
理解同步,异步和延迟脚本
查看>>
MMS源码中异步处理简析
查看>>
XMind 6 如何画流程图
查看>>
final发布评价
查看>>