HDU 1029 Ignatius and the Princess IV - Problem A
来源:互联网 发布:嵌入式linux开机动画 编辑:程序博客网 时间:2024/05/20 14:19
// AKC++.cpp : 定义控制台应用程序的入口点。////#include "stdafx.h"#include <iostream>#include <cstdio>#include <string>#include <cmath>#define MAX 1000005using namespace std;/**题意:找出N(奇数)个数中, 个数大于(N-1)/2的数分析1(TLE): 显然这些数必有一个在(N+1)/2这个位置, 用第快拍的思想找出第K大,虽然复杂度接近O(n) 但是可惜超时了分析2(AC): 哈希的思想, 输入一个就记录数字的个数+1, 如果有数字个数超过一半就不用再记了, 这样就快的多了. 在没给数据范围的情况下, 数组记录可能会溢出, 字典树是个不错的选择~算法: trie树利用重复前缀的思想,使得存储空间极大节约, 存取过程为O(len) , len为记录的长度; */int N;int a[MAX];bool cmp(const int &lowVal, const int &highVal){ return lowVal <= highVal;}int partition(int low, int high){ int key = a[low]; while(low < high){ while( low < high && cmp(key, a[high])){high--;}a[low] = a[high]; while( low < high && !cmp(key, a[low])){low ++;}a[high] = a[low]; }a[low] = key;///在结果位置放入key值 return low;}void qSort(int low, int high){ if(low < high){ int mid = partition(low, high); qSort(low, mid); qSort(mid + 1, high); }}void find_kthMax(int low, int high, int k){ if(low < high){ int mid = partition(low, high); int kk = high - mid + 1; if(kk == k) return ; else if(kk < k) find_kthMax(low, mid - 1, k - kk); else find_kthMax(mid + 1, high, k); }}///------------------trie-------///跟hash思想一样, 但是利用重复前缀的特点, 极大节约了空间const int trieSize = 10;//数字字典10个分叉char num[20];bool isFind;char ans[20];struct Node{ int times; Node *ChildNode[trieSize]; Node( ){ times = 0; for( int i = 0; i < trieSize; i++){ ChildNode[i] = NULL; } }};void insertNum(char* num, int len, Node *father, int i){ int ii = num[i] - '0'; if(father->ChildNode[ii]==NULL){ father->ChildNode[ii] = new Node( ); } if(len == 1){ father->ChildNode[ii]->times++; if(father->ChildNode[ii]->times >= N/2 + 1){ isFind = true; strcpy(ans, num); } } else insertNum(num, len -1, father->ChildNode[ii], i + 1);}int main(){ while(cin>>N){ Node *root = new Node();///暂时不考虑空间的释放 isFind = false; for( int i = 0; i < N; i++){ cin>>num; if(!isFind) insertNum(num, strlen(num), root, 0); } cout<<ans<<endl; } return 0;}
- HDU 1029 Ignatius and the Princess IV - Problem A
- hdu 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- Ignatius and the Princess IV hdu 1029
- hdu 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- HDU-1029-Ignatius and the Princess IV
- HDU--1029--Ignatius and the Princess IV
- opencv 移植到ti cortex a8
- STK:在STK中使用天线对象
- PCI设备介绍(一):PCI的一些数字
- sql server 2005 加载程序集 CREATE ASSEMBLY
- zoom 在css中的意思?
- HDU 1029 Ignatius and the Princess IV - Problem A
- 关于更新发布CSS和JS文件的缓存问题
- storm 实战及实例讲解(一)
- SQL删除重复数据只保留一条
- c语言模拟小球落地
- Hibernate_get和load的区别
- 使用 Eclipse Memory Analyzer 进行堆转储文件分析
- android 字符编码转换
- 添加应用程序到系统功能