找出该字符串中出现次数最多的那个字符

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

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

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

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

C++实现

#include<iostream>
using namespace std;

int main()
{
    char c[60000] = { 0 };
    char s[5] = { 0 };
    unsigned int count[52] = { 0 };
    char output[52] = { 0 };
    memset(c, 0, 60000);
    bool sensitive = true;
    cin >> c;
    cin >> s;
    int i = 0;
    int index = 0;
    while ((i < 5) && (s[i] != 0))
    {
        if (strcmp(&s[i + 1], "true"))
        {
            sensitive = true;
        }
        else
        {
            sensitive = false;
        }
        break;

        i++;
    }
    int j = 0;
    int max = 0, outputIndex = 0;
    while ((j < 6000)&&(c[j]!=0))
    {
        if (sensitive)
        {
            if (90 < c[j])
            {
                index = c[j] - ‘A‘;
                count[index]++;
                if (output[index] == 0)
                {
                    output[index] = c[j];
                }
                if (max < count[index])
                {
                    max = count[index];
                    outputIndex = index;
                }
            }
            else
            {
                index = c[j] - ‘a‘;
                count[index + 26]++;
                if (output[index + 26] == 0)
                {
                    output[index + 26] = c[j];
                }
                if (max < count[index + 26])
                {
                    max = count[index + 26];
                    outputIndex = index + 26;
                }
            }
        }
        else
        {
            if (90 < c[j])
            {
                index = c[j] - ‘A‘;
            }
            else
            {
                index = c[j] - ‘a‘;
            }
            count[index]++;
            if (output[index] == 0)
            {
                output[index] = c[j];
            }
            if (max < count[index])
            {
                max = count[index];
                outputIndex = index;
            }
        }
        j++;
    }
    cout << output[outputIndex] << " " << max << endl;
    system("pause");
    return 0;
}

注意编译器不兼容的问题

原文地址:https://www.cnblogs.com/UFO-blogs/p/8525839.html

时间: 2024-07-30 17:08:27

找出该字符串中出现次数最多的那个字符的相关文章

[PY3]——找出一个序列中出现次数最多的元素/collections.Counter 类的用法

问题 怎样找出一个序列中出现次数最多的元素呢? 解决方案 collections.Counter 类就是专门为这类问题而设计的, 它甚至有一个有用的 most_common() 方法直接给了你答案 collections.Counter 类 1. most_common(n)统计top_n from collections import Counter words = [ 'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes', 't

1152: 零起点学算法59——找出一个数组中出现次数最多的那个元素

1152: 零起点学算法59--找出一个数组中出现次数最多的那个元素 Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lldSubmitted: 990  Accepted: 532[Submit][Status][Web Board] Description 找出一个数组中出现次数最多的那个元素 Input 第一行输入一个整数n(不大于20) 第二行输入n个整数 多组数据 Output 找出n个整数中出现次数最多的那个整数(

[email&#160;protected]返回字符串中出现次数最多的那个字符和次数2

1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title>@返回字符串中出现次数最多的那个字符和次数2</title> 6 7 </head> 8 <body> 9 </body> 10 11 <script type="text/javasc

python 找出一篇文章中出现次数最多的10个单词

#!/usr/bin/python #Filename: readlinepy.py import sys,re urldir=r"C:\python27\a.txt" distone={} numTen=[] #先文档变成一个字典 f=open(urldir,'r') for line in f.readlines(): #去掉非字符的符号 line = re.sub('\W'," ",line) lineone=line.split() for keyone i

找出一个数组中出现次数最多的那个元素

#include <stdio.h> int main() { int n,a[20],i,j,flag=0,max; int b[20]={0}; while(scanf("%d",&n)==1){ if(n==0) break; for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(a[i]==a[j]) b[i]++

给定一个字符串,找出这个字符串中首先出现K次的字符

public static Character FindFirstKChar(String str,int k){ String s = str.replaceAll(" ","");         Map<Character, Integer> charCountMap = new HashMap<Character, Integer>();         Character ch = null;         for(int i =

找出n个字符串中出现次数最多的字符串。

1. 找出n个字符串中出现次数最多的字符串. C/C++: char* find(char **data,int n); Java: String find(String data[]); 说明: 1. data是字符串数组,n是数组中字符串的个数,返回值为出现次数最多的字符串. 2. 若结果有多个,返回任意一个即可 3. 不得使用任何库函数/API,如需使用类似功能, 请自行实现 4. 算法效率尽可能高,尽量少的使用内存空间 5. 必须要有代码注释和算法说明. 例如:data里面的数据是{“p

找出字符串中出现次数最多的字符,和最大次数

/*找出字符串中出现次数最多的字符,和最大次数*/ function countMax(str){ var max = 0; // 记录出现的最大次数 var maxChar = ""; // 记录出现最多次数的字符 var counts = new Array(127); // 记录中间计算结果 for(var i = 0; i < counts.length; i++){ counts[i] = 0; } for(var i = 0; i < str.length; i

找出一个字符串中最长重复次数的子字符串,并计算其重复次数

原题 找出一个字符串中最长重复次数的子字符串,并计算其重复次数.例如:字符串"abc fghi bc kl abcd lkm abcdefg",并返回"abcd"和2. 我的思路 为了方便表述,我们使用变量src作为原字符串,sub_str作为子字符串. 由于题目要求寻找至少重复2次的最长的子字符串,重点在于最长的子字符串,而不在于重复的最多次数.因此我们可以从长度最长的字符串入手,计算其重复次数.只要重复达到2次,即可返回该字符串. 显然长度最长的子字符串就是原串