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/|

  • 相关阅读:
    chrome的javascript 中的一个奇怪现象,引申到javascript的interger存储机制,ECMA standerd script的int engine分析
    php中的header("Location:URL") 与 javascript中 window.localtion 的区别
    test
    str_replace() 中的参数 类型、反反斜杠
    从PHP代码分析PHP 的GC(垃圾回收) 机制
    分离
    fiddler 监听某一站点
    PHP中对象的clone和引用的区别(Object Cloning and Passing by Reference in PHP)
    IOS使用MessageUI Framework 发送邮件
    IOS使用MessageUI Framework 发送短信息
  • 原文地址:https://www.cnblogs.com/icurious/p/6082231.html
Copyright © 2011-2022 走看看