杭电1004

题目描述:
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. It is guaranteed that there is a unique solution for each test case.

Sample Input
5
green
red
blue
red
red
3
pink
orange
pink
0

Sample Output
red
pink

难点在于:不能预先知道有多少种颜色,所以无法进行针对性的内存分配,并且每次输入一个颜色,都要与已有颜色尝试匹配。

思路:对每一次输入的颜色,都要跟已有的颜色进行比较。如果与已经存在的某个单词的长度与输入的单词颜色相同,就按位比较,每一位都相同,就说明这个颜色出现过,那么将该颜色的数目加一,只要在按位比较的过程中,出现了一个字符不一样,就立马换下一个已有单词与输入的单词比较;如果将所有的已存在的单词都比较过了,没有发现与输入的单词完全相同的单词,说明这是个新的颜色,要将这个新颜色插到store[]数组末尾,并且记录它的长度信息,以及终止位置。最后将对每个颜色进行计数的数组排序,排序过程中,每进行一次位置变动,就要将颜色的索引进行变动,最后得出数量最多的颜色,是第几种颜色。然后在store[]数组里,输出这个颜色的所有字符。

#include <stdio.h>
#include <string.h>
int main()
{
    char store[15000];//每发现一种新颜色,就插入到末尾

    int start;//某颜色在store[]中的开始位置
    int stop[1000];//某颜色在store[]中的结束位置
    int len[1000];//某颜色的单词长度
    int cnt[1000];//某颜色的数量
    int index[1000];//某颜色进入store[]的次序

    int ballon,ballons;

    int colors=0;//颜色总量

    char temp[15];//up to 15,每个气球的颜色
    int templen;//每个气球的单词长度

    int i,j,k,p,q;

    int exchange;//用作排序时的交换中介

    while(1)
    {
        scanf("%d",&ballons);//气球总数
        if(ballons==0)//输入0,结束
            return 0;
        else
        {
            getchar();
            for(ballon=0;ballon<ballons;ballon++)
            {
                gets(temp);//输入每个气球的单词
                templen = strlen(temp);//获取每个气球的单词长度

                for(i=0;;i++)//检测每个已有颜色
                {
                    if(i==0)//规定开始与结束位置
                        start = 0;
                    else
                        start = stop[i-1];
                    if(len[i]==0)
                    //如果匹配到了长度为零颜色,说明没有颜色与输入色相同,这是个新颜色
                    {
                        colors++;//颜色总数自加1
                        cnt[i]++;//该颜色的数量加1
                        len[i] = templen;//该颜色的长度信息录入
                        stop[i] = start + templen;//结束位置
                        for(j=0;j<templen;j++)//录入store[]数组
                            store[start+j] = temp[j];
                        break;//针对这个气球的处理过程结束
                    }
                    if(templen==len[i])//尝试匹配时,若长度相同
                    {
                        for(k=0;k<templen;k++)//从前到后一个个比较
                        {
                            if(temp[k]!=store[start+k])
                                break;//出现字符不相等,不用再比了
                        }
                        if(k==templen)// attention
                        //如果到最后k=templen-1的时候,对应字符都是相等的,说明这两个单词
                        //就相等,而循环结束后k要自加1,所以此时k==templen
                        {
                            cnt[i] += 1;//数目加一
                            break;//针对这个气球的处理结束
                        }
                    }
                }
            }

            for(i=0;i<colors;i++)
                index[i] = i;//获得每个颜色的次序

            for(p=0;p<colors-1;p++)//let index[0] store the index of the most
            //popular color,冒泡排序,由大到小
            {
                for(q=0;q<colors-p-1;q++)
                    if(cnt[q]<cnt[q+1])
                    {//次序索引对换,计数对换
                        exchange = index[q];
                        index[q] = index[q+1];
                        index[q+1] = exchange;

                        exchange = cnt[q];
                        cnt[q] = cnt[q+1];
                        cnt[q+1] = exchange;
                    }
            }
            i = index[0];//次序索引数组第一个存的就是最popular的颜色的次序

            if(i==0)
                start = 0;
            else
                start = stop[i-1];

            for(j=start;j<start+len[i];j++)//逐位输出
            {
                printf("%c",store[j]);
            }

            if(colors>0)
                printf("\n");

            for(i=0;i<colors;i++)//这一轮输出结束,要将相关信息清零
            //store[]数组不用清,因为所有的比较,输出都是根据以下几个数组来的
              len[i] = stop[i] = cnt[i] = index[i] = 0;
        }
    }
}

这次终于是一次就accepted,还可以。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值