HDU 1004(字典树,c++ map)
来源:互联网 发布:查看淘宝店铺信誉等级 编辑:程序博客网 时间:2024/05/16 09:45
题意:给出一个n与n个颜色,求出现次数最多的颜色。
暴力方法:
#include <stdio.h>#include <string.h>int main(){ int n,i,max=1,count=0,flag,j; while(1) { char a[1000][50]; max=1; scanf("%d",&n); if(n==0) break; getchar(); for(i=0;i<n;i++) gets(a[i]); if(n==1) { puts(a[0]); continue; } for(i=0;i<n;i++) { count=0; for(j=0;j<n;j++) { if(strcmp(a[i],a[j])==0) count++; } if(count>max) { max=count; flag=i; } } puts(a[flag]); } return 0;}
c++ map的使用:
#include <cstdio>#include <map>#include <iostream>#include <string>using namespace std;void main(){ int N; string ballon; while (cin >> N, N) { map<string, int> m; int n = N; while (n--) { cin >> ballon; ++m[ballon]; } map<string, int>::iterator p; int max = 0; string max_ballon; for (p = m.begin(); p != m.end(); ++p) { if (p->second > max) { max = p->second; max_ballon = p->first; } } cout << max_ballon << endl; }}
字典树的使用:
#include <iostream>#include <cstring>#include <algorithm>#include <string>#include <cstdio>using namespace std;struct Trie{ int base; Trie* next[26]; Trie() : base(-1) { memset(next, NULL, sizeof next); }};string color[1002];int _count[1002];int cnt;void insert(Trie* root, string& line){ Trie* p = root; int len = line.length(), n; for (int i = 0; i < len; ++i) { n = line[i] - 'a'; if (p->next[n] == NULL) p->next[n] = new Trie; p = p->next[n]; } if (p->base == -1) { p->base = cnt; color[cnt++] = line; } ++_count[p->base];}void freeTrie(Trie* root){ for (int i = 0; i < 26; ++i) if (root->next[i]) freeTrie(root->next[i]); delete root;}int main(){ int n, i; string line; Trie* root; while (cin >> n && n) { root = new Trie; cnt = 0; memset(_count, 0, sizeof _count); for (i = 0; i < n; ++i) { cin >> line; insert(root, line); } int _max = 0, base; for (i = 0; i < cnt; ++i) if (_count[i] > _max) { _max = _count[i]; base = i; } cout << color[base] << endl; freeTrie(root); }}
0 0
- HDU 1004(字典树,c++ map)
- HDU 1004 (字典树||map)
- hdu 1075 字典树 和 map做法
- 字典树 ,map 容器 hdu 1251
- HDU 4287 Intelligent IME (字典树 && map)
- HDU 1075 翻译 (字典树||map)
- HDU-1004-提供四种解法--map解法/自己写二叉排序树/hash/字典树
- Hat’s Words hdu 1247(字典树 map)
- hdu 1247 字典树以及map+string 2种做法
- hdu-1247 简单map的应用。(字典树)
- [字典树、map] HDU 1800 - Flying to the Mars
- HDU 1247 Hat’s Words (字典树 && map)
- HDU 1251 (统计难题) 字典树模板&&map实现
- HDU 1251:统计难题【字典树 & string+map】
- HDU-1251 统计难题(字典树+map)
- 【HDU】1251 - 统计难题(字典树 || STL - map & string)
- HDU-1251-统计难题(字典树||map)
- HDU-1251 统计难题,字典树或者map!
- svchost.exe占满一个核心的资源
- 记录u-boot不能引导内核的解决过程
- 报数游戏(二)
- C#反射技术概念作用和要点
- GetWindowRect和GetClientRect
- HDU 1004(字典树,c++ map)
- php内核探索:创建对象实例
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Android UI 设计之 TextView EditText 组件属性方法最详细解析
- 惯性导航
- 冒泡排序 --学习(二)
- Linux I/O Scheduler--CFQ
- php对象属性读写
- linux基础命令学习笔记