HDU 1800 简单贪心 13.1.10用trie重做 ---其实是统计出现次数最多的单词
2012-09-12 11:04
405 查看
#include <iostream> #include <algorithm> using namespace std; const int N = 3010; int a ; int visit ; int main() { int n; while(cin >> n) { memset(a, 0, sizeof(a)); memset(visit, 0, sizeof(visit)); for(int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a+n); int cnt = 0; //扫走数 for(int i = 0; i < n; i++) { if(visit[i] == 1) continue; int pri = a[i]; cnt++; for(int j = i + 1; j < n && visit[i] == 0; j++) { if(a[j] > pri && visit[j] != 1) { visit[j] = 1; pri = a[j]; } } } cout << cnt << endl; } return 0; }
2013.1.10
trie实现。
其实题意就是统计出现最多次的的数字
用map做的ac代码:
#include <iostream> #include <map> using namespace std; int main() { int n; while(cin >> n) { map<int, int> bm; int temp; for(int i = 0; i < n; i++) { scanf("%d", &temp); bm[temp]++; } int maxval = -1; for(map<int, int>::iterator iter = bm.begin(); iter != bm.end(); ++iter) { if(iter->second > maxval) maxval = iter->second; } cout << maxval << endl; } return 0; }
用trie的ac代码:
#include <iostream> #include <cstdio> #include <string.h> #include <string> #include <malloc.h> using namespace std; int zero = 0; struct Trie { int count; struct Trie* trie[10]; }*root; void init() { root = new Trie; for(int i = 0; i < 10; i++) root->trie[i] = NULL; } int insert(char s[]) { Trie * head = root; int len = strlen(s); int j = -1; for(int i = 0; i < len; i++) { //将数字前面的0除去 if(s[i] != '0') { j = i; break; } } if(j == -1) { zero++; return zero; } for(int i = j; i < len; i++) { int k = s[i] - '0'; if(head->trie[k] != NULL) { head = head->trie[k]; } else { head->trie[k] = new Trie; head = head->trie[k]; head->count = 0; for(int z = 0; z < 10; z++) head->trie[z] = NULL; } } head->count++; return head->count; } void del(Trie* p) { for(int i = 0; i < 10; i++) { if(p->trie[i] != NULL) { del(p->trie[i]); } } delete p; } int main() { int count; while(~scanf("%d", &count)) { zero = 0; init(); char ss[31]; int max = 0; while(count--) { scanf("%s", ss); int num = insert(ss); if(num > max) max = num; } printf("%d\n", max); del(root); } return 0; }
相关文章推荐
- Java统计一篇文章中出现次数最多的汉字或英文单词 又出现次数的统计
- sort +awk+uniq 统计文件中出现次数最多的前10个单词
- 用python统计文本里的单词出现次数最多的10个
- 统计出现次数最多的单词
- NIO MappedByteBuffer读大文件并统计出现次数最多的TOP K个单词
- sort +awk+uniq 统计文件中出现次数最多的前10个单词
- Linux命令经典面试题:统计文件中出现次数最多的前10个单词
- 意外收获-ruby编写的一个简单统计单词出现次数的程序
- 华为:统计一串字符串中出现次数最多和次多的单词(华为上机考试题)
- 统计一串字符串中出现次数最多和次多的单词
- 统计一串字符串中出现次数最多和次多的单词(华为上机考试题)
- 转载:统计一串英文中出现次数最多的单词以及次数;
- sort +awk+uniq 统计文件中出现次数最多的前10个单词yes3
- 统计连续出现次数最多的单词
- Java统计英文句子中出现次数最多的单词并计算出现次数的方法
- 微软笔试题 统计英文电子书中出现次数最多的k个单词
- 统计单词出现的最多次数(Trie树)
- sort +awk+uniq 统计文件中出现次数最多的前10个单词 (2012-12-05 15:57:12)转载▼ 标签: 转载 原文地址:sort +awk+uniq 统计文件中出现次数最多的前
- 统计文本中单词出现次数,打印出现次数最多的前10个.文本大小30KB~300KB
- 一个简单的例子理解C++ map, 运用map统计单词出现的次数