• 给定一个长度不限的字符串,找出该字符串中出现次数最多的字符


    /*
    时间限制 C/C++ 3s 其他 6s, 空间限制 C/C++ 32768k 其他 65535k

    题目描述
        给定一个长度不限的字符串,请找出该字符串中出现次数最多的那个字符,并打印出该字符及其出现次数;
     如果多个字符的出 现次数相同,只打印首个字符;输出字符的大小写格式要与输 入保持一致,大小写不敏感模式下,
     输出字符的大小写格式与该 字符首次出现时的大小写格式一致。实现时无需考虑非法输。

    输入描述
        输入为 字符串大小写敏感标记 其中"大小写敏感标记"为可选参数,取值范围为七yue|1fa1 se,txue表示大小写敏感;缺省取值txue
     例子: abcdabcde fa1e

    输出描述
        输出:字符出现次数 如果出现次数最多的字符有多个,输出字典序最小的那个字 符。输出的字符都为小写字符
     例子: a 2
    */

    Python实现

     1 str1 = input()
     2 str2 = str1.split(" ")
     3 sensitive = True
     4 if str2[1] == "false":
     5     sensitive = False
     6 maxCount = 0
     7 maxChar = "0"
     8 if sensitive:
     9     for c in str2[0]:
    10         tmp = str2[0].count(c)
    11         if tmp > maxCount:
    12             maxCount = tmp
    13             maxChar = c
    14 else:
    15     lc = str2[0].lower()
    16     index = 0;
    17     for c in lc:
    18         tmp = lc.count(c)
    19         if tmp > maxCount:
    20             maxCount = tmp
    21             maxChar = str2[0][index]
    22         index += 1
    23 print("%s %d" % (maxChar, maxCount))

    C++实现,好像最终还有点问题。

     1 #include<iostream>
     2 using namespace std;
     3 
     4 int main()
     5 {
     6     char c[60000] = { 0 };
     7     char s[5] = { 0 };
     8     unsigned int count[52] = { 0 };
     9     char output[52] = { 0 };
    10     memset(c, 0, 60000);
    11     bool sensitive = true;
    12     cin >> c;
    13     cin >> s;
    14     int i = 0;
    15     int index = 0;
    16     while ((i < 5) && (s[i] != 0))
    17     {
    18         if (strcmp(&s[i + 1], "true"))
    19         {
    20             sensitive = true;
    21         }
    22         else
    23         {
    24             sensitive = false;
    25         }
    26         break;
    27 
    28         i++;
    29     }
    30     int j = 0;
    31     int max = 0, outputIndex = 0;
    32     while ((j < 6000)&&(c[j]!=0))
    33     {
    34         if (sensitive)
    35         {
    36             if (90 < c[j])
    37             {
    38                 index = c[j] - 'A';
    39                 count[index]++;
    40                 if (output[index] == 0)
    41                 {
    42                     output[index] = c[j];
    43                 }
    44                 if (max < count[index])
    45                 {
    46                     max = count[index];
    47                     outputIndex = index;
    48                 }
    49             }
    50             else
    51             {
    52                 index = c[j] - 'a';
    53                 count[index + 26]++;
    54                 if (output[index + 26] == 0)
    55                 {
    56                     output[index + 26] = c[j];
    57                 }
    58                 if (max < count[index + 26])
    59                 {
    60                     max = count[index + 26];
    61                     outputIndex = index + 26;
    62                 }
    63             }
    64         }
    65         else
    66         {
    67             if (90 < c[j])
    68             {
    69                 index = c[j] - 'A';
    70             }
    71             else
    72             {
    73                 index = c[j] - 'a';
    74             }
    75             count[index]++;
    76             if (output[index] == 0)
    77             {
    78                 output[index] = c[j];
    79             }
    80             if (max < count[index])
    81             {
    82                 max = count[index];
    83                 outputIndex = index;
    84             }
    85         }
    86         j++;
    87     }
    88     cout << output[outputIndex] << " " << max << endl;
    89     system("pause");
    90     return 0;
    91 }
    
    

    注意编译器不兼容的问题

  • 相关阅读:
    [PHP]AES加密----PHP服务端和Android客户端
    [PHP]memcache安装
    [Android]apk反编译方法
    [PHP]生成随机数(建立字典)
    [PHP]Mysql的运用
    [PHP]对象数组和普通数组总结
    ThinkPHP5+Redis单例型购物车
    移动硬盘新建选项消失、不能新建文件夹和文件的解决方案
    PHP substr() 函数截取中文字符串乱码
    php开发中遇到问题的找错误的方法
  • 原文地址:https://www.cnblogs.com/UFO-blogs/p/8525839.html
Copyright © 2020-2023  润新知