weixin_53185809 2023-08-06 15:06 采纳率: 90.9%
浏览 9
已结题

关于#字符串#的问题,如何解决?

题目描述
回文字符串是指一个字符串从左往右读和从右往左读是相同的,例如:abcba,aba等。
现在小图灵得到了T个仅包含小写字母的字符串,他想知道对于每个字符串来说,能否重新组合得到K个回文字符串。请你帮助他解决
问题。
输入描述
第1行包括一个整数工,代表字符串的个数。
第2~T+1行包含一个仅含有小写字母的字符串,每个字符串的长度都不超过n。
输出描述
输出共T行,对于每个字符串来说,结果输出在一行上。若重组得到回文字符串有K个,当K=1时,输出onlyone,当K=2时,
输出 only two;当K>2时,输出more;若不能得到回文字符串则输出 no。
一开始是想着求计数个字母的个数,但是不对


```c++
#include <iostream>
#include <string>
#include <unordered_map>

using namespace std;

bool canFormPalindromes(string str) {
    unordered_map<char, int> count;
    for (char c : str) {
        count[c]++;
    }
    int oddCount = 0;
    for (auto entry : count) {
        if (entry.second % 2 != 0) {
            oddCount++;
        }
    }
    return oddCount <= 1;
}

string getPalindromeCount(string str) {
    if (canFormPalindromes(str)) {
        unordered_map<char, int> count;
        for (char c : str) {
            count[c]++;
        }
        int distinctChars = count.size();
        if (distinctChars == 1) {
            return "only one";
        } else if (distinctChars == 2) {
            return "only two";
        } else {
            return "more";
        }
    } else {
        return "no";
    }
}

int main() {
    int T;
    cin >> T;

    for (int i = 0; i < T; i++) {
        string str;
        cin >> str;

        cout << getPalindromeCount(str) << endl;
    }

    return 0;
}


还有这一版
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

bool canFormPalindromes(string str) {
    vector<int> count(26, 0);
    int oddCount = 0;

    for (char c : str) {
        count[c - 'a']++;
    }

    for (int freq : count) {
        if (freq % 2 != 0) {
            oddCount++;
        }
    }

    return oddCount <= 1;
}

string getPalindromeCount(string str) {
    if (canFormPalindromes(str)) {
        int distinctChars = count_if(str.begin(), str.end(), [&](char c) {
            return count(str.begin(), str.end(), c) % 2 != 0;
        });

        if (distinctChars == 1 || distinctChars == 2) {
            return "only two";
        } else {
            return "more";
        }
    } else {
        return "no";
    }
}

int main() {
    int T;
    cin >> T;

    for (int i = 0; i < T; i++) {
        string str;
        cin >> str;

        cout << getPalindromeCount(str) << endl;
    }

    return 0;
}

```

  • 写回答

2条回答 默认 最新

  • 不良使 Python领域潜力新星 2023-08-06 15:47
    关注
    
    #include <iostream>
    #include <string>
    #include <unordered_map>
    using namespace std;
    
    bool canFormPalindromes(string str) {
        unordered_map<char, int> count;
        for (char c : str) {
            count[c]++;
        }
        int oddCount = 0;
        for (auto entry : count) {
            if (entry.second % 2 != 0) {
                oddCount++;
            }
        }
        return oddCount <= 1;}
    string getPalindromeCount(string str) {
        if (canFormPalindromes(str)) {
            unordered_map<char, int> count;
            for (char c : str) {
                count[c]++;
            }
            int distinctChars = count.size();
            if (distinctChars == 1) {
                return "onlyone";
            } else if (distinctChars == 2) {
                return "onlytwo";
            } else {
                return "more";
            }
        } else {
            return "no";
        }
    }
    int main() {
        int T;
        cin >> T;
        for (int i = 0; i < T; i++) {
            string str;
            cin >> str;
            cout << getPalindromeCount(str) << endl;
        }
    
        return 0;
    }
    

    看看这个对你有没有帮助

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月24日
  • 已采纳回答 8月16日
  • 修改了问题 8月6日
  • 创建了问题 8月6日

悬赏问题

  • ¥15 angular项目错误
  • ¥20 需要帮我远程操控一下,运行一下我的那个代码,我觉得我无能为力了
  • ¥20 有偿:在ubuntu上安装arduino以及其常用库文件。
  • ¥15 请问用arcgis处理一些数据和图形,通常里面有一个根据点划泰森多边形的命令,直接划的弊端是只能执行一个完整的边界,但是我们有时候会用到需要在有很多边界内利用点来执行划泰森多边形的命令
  • ¥30 在wave2foam中执行setWaveField时遇到了如下的浮点异常问题,请问该如何解决呢?
  • ¥750 关于一道数论方面的问题,求解答!(关键词-数学方法)
  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件