• StringDup(理论AC)


    Problem Statement for StringDup

    Problem Statement

        
    Create a class called StringDup. Given a string made up of ONLY letters and
    digits, determine which character is repeated the most in the string ('A' is
    different than 'a'). If there is a tie, the character which appears first in
    the string (from left to right) should be returned.
    
    Examples :
    
    aaiicccnn = c
    aabbccdd = a
    ab2sbf2dj2skl = 2
    
    Here is the method signature :
    
    public char getMax(String input);
    
    We will check to make sure that the input contains only letters and digits (no
    punctuation marks or spaces).
     

    Definition

        
    Class: StringDup
    Method: getMax
    Parameters: String
    Returns: char
    Method signature: char getMax(String param0)
    (be sure your method is public)
        
     

    This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2010, TopCoder, Inc. All rights reserved. 

    /*
    * @Author: LyuCheng
    * @Date:   2017-05-13 20:02:42
    * @Last Modified by:   LyuCheng
    * @Last Modified time: 2017-05-13 20:40:49
    */
    /*
    题意:给你一个只有字母和数字组成的字符串,让你输出出现次数最多的字符,如果最多数量的相同
        就输出最先出现的那个字符
    
    思路:结构体加映射,结构体记录字符出现的顺序和出现的次数,然后按照次数为第一优先级,顺序
        第二优先级进行排序输出。
    */
    #include <iostream>
    #include <stdio.h>
    #include <algorithm>
    #include <string.h>
    using namespace std;
    struct node{
        int rank;
        int res;
        char str;
        node(){}
        node(int _rank,int _res,char _str){
            rank=_rank;
            res=_res;
            str=_str;
        }
        bool operator <(const node &other) const{
            if(res==other.res) return rank<other.rank;
            else return res>other.res;
        }
    };
    int pos=0;//字符的种类
    node vis[65];
    char str[1000005];
    void init(){
        pos=0;
        for(int i=0;i<65;i++){
            vis[i].str='';
            vis[i].res=0;
            vis[i].rank=65;
        }
    }
    int main(){
        freopen("in.txt","r",stdin);
        while(scanf("%s",str)!=EOF){
            init();
            int n=strlen(str);
            for(int i=0;i<n;i++){
                if(str[i]>='0'&&str[i]<='9'){
                    if(vis[str[i]-'0'].rank==65){
                        vis[str[i]-'0'].str=str[i];
                        vis[str[i]-'0'].rank=pos++;
                        vis[str[i]-'0'].res++;
                    }else{
                        vis[str[i]-'0'].res++;
                    }
                }else if(str[i]>='a'&&str[i]<='z'){
                    if(vis[str[i]-'a'+10].rank==65){
                        vis[str[i]-'a'+10].str=str[i];
                        vis[str[i]-'a'+10].rank=pos++;
                        vis[str[i]-'a'+10].res++;
                    }else{
                        vis[str[i]-'a'+10].res++;
                    }
                }else{
                    if(vis[str[i]-'A'+36].rank==65){
                        vis[str[i]-'A'+36].str=str[i];
                        vis[str[i]-'A'+36].rank=pos++;
                        vis[str[i]-'A'+36].res++;
                    }else{
                        vis[str[i]-'A'+36].res++;
                    }
                }
            }
            sort(vis,vis+61);
            printf("%c
    ",vis[0].str);
        }
        return 0;
    }
  • 相关阅读:
    nexus 手动更改 私服包
    maven 构建时 错误: 程序包netscape.javascript不存在
    RocketMQ
    NSQ
    beego 实现API自动化文档
    动态追踪技术漫谈
    go vendor管理Golang项目依赖
    consul介绍
    golang rpc介绍
    golang 使用os/exec配合context实现的超时机制
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6850365.html
Copyright © 2020-2023  润新知