1. 程式人生 > >找出該字符串中出現次數最多的那個字符

找出該字符串中出現次數最多的那個字符

esp 表示 輸出 ive 出現次數 output post 字典 如果

/*
時間限制 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; }

註意編譯器不兼容的問題

找出該字符串中出現次數最多的那個字符