zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    update语句中存在''语法书写方式
    CSS的代码风格
    CSS的语法规范
    CSS层叠样式表导读
    CSS简介
    HTML基本标签(下)
    HTML基本标签(上)
    HTML简介导读
    集合及其运用
    字典的镶嵌
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6850365.html
Copyright © 2011-2022 走看看