zoukankan      html  css  js  c++  java
  • 【校算法训练解题报告1】-Problem A: Let the Balloon Rise

    Problem A: Let the Balloon Rise


    版权声明:本文为 @iCurious
    的原创文章,可以转载,但请务必注明作者和出处!!!
    原文链接:|blog.csdn.net/icurious|www.blankspace.cn|www.cnblogs.com/icurious/|

    Problem A: Let the Balloon Rise

    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

    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.

    Sample Input

    5
    green
    red
    blue
    red
    red
    3
    pink
    orange
    pink
    0

    Sample Output

    red
    pink

    算法/思路

    题目的意思实际输入一系列的字符串,代表ACM竞赛中升起气球的颜色,要做的就是输出出现次数最多的字符串。如果有多个则按照字母表顺序输出。
    实质:统计字符串个数
    我开始采用的是先统计个数再排序的思路:没出现一个单词,则对其个数进行+1,最后的按照字母表顺序输出出现次数最多的单词,但比较低效和耗费空间。所以换一种思路,先对输入的字符串数组进行排序再统计各个字符串出现的次数和去重处理

    算法实现

    先排序,再去重

    #include <iostream>
    #include <string>
    using namespace std;
    
    void StrSort(string s[],int lo,int hi)
    {
    	while(lo<--hi)
    	{
    		int mx=0;
    		for(int i=lo+1;i<=hi;i++)
    		{
    			if(s[mx]<s[i])
    				mx=i;//i就是每一趟最大值得下标
    		}
    		//cout<<s[mx]<<"<--swap_with-->"<<s[hi]<<endl;
    		swap(s[mx],s[hi]);	
    	}
    }
    
    int  main()
    {
    	int N;
    	int i=0;
    	//cin>>N;
    	while(cin>>N&&N!=0)
    	{
    		string* s=new string[N];//用来存储各种颜色字符串的动态空间 
    		for(i=0;i<N;i++){cin>>s[i];}//cout<<"s["<<i<<"]="<<s[i]<<endl;
    		StrSort(s,0,N); 
    		//for(i=0;i<N;i++){cout<<s[i]<<endl;}
    		
    		int* count=new int[N];//去重 
    		for(i=0;i<N;i++){count[i]=0;}
    		int l=0,r=0;
    		while(++r<N)
    		{
    			if(s[l]==s[r])count[l]++;
    			else {s[++l]=s[r];}
    		}
    		
    		int size=l+1;//当前元素个数 
    		int mx=0;
    		//cout<<"size="<<size<<endl;
    		for(i=0;i<size;i++)
    		{
    			//cout<<"count[]="<<count[i]<<" ";
    			if(count[mx]<count[i])mx=i;	
    			//cout<<"
    mx="<<mx<<endl;
    		}
    		for(i=0;i<size;i++)
    		{
    			if(count[mx]==count[i])cout<<s[i]<<endl;	
    		}			
    		delete []count;		
    		delete []s;		
    	}
    	return 0;
    }
    
    

    版权声明:本文为 @iCurious
    的原创文章,可以转载,但请务必注明作者和出处!!!
    原文链接:|blog.csdn.net/icurious|www.blankspace.cn|www.cnblogs.com/icurious/|

  • 相关阅读:
    dubbo-Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError
    sqlmap基本命令
    Read-Only Tables 只读表
    Oracle Set操作
    OGG-00664
    Oracle 附加日志(supplemental log)
    Oracle 数字转为字符串 to_char()
    Oracle单引号转义符
    OGG开启DDL,约束信息和索引信息会同步过去吗
    OGG-01161
  • 原文地址:https://www.cnblogs.com/icurious/p/6082231.html
Copyright © 2011-2022 走看看