C++ 字符串处理程序:删除连续字符、查找最长/最频繁单词、最大公共子序列

本 C++ 程序实现了三个字符串处理功能:

  1. 删除字符串中连续出现次数超过 2 的字符。
  2. 查找字符串中最长单词和重复次数最多的单词。
  3. 找出两个字符串之间的最大公共子序列。
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

// 删除连续出现次数超过2的字符
std::string deleteConsecutiveChars(const std::string& input) {
    std::string result;
    int count = 1;
    for (int i = 0; i < input.length(); i++) {
        if (i < input.length() - 1 && input[i] == input[i + 1]) {
            count++;
        } else {
            if (count <= 2) {
                result += input[i];
            }
            count = 1;
        }
    }
    return result;
}

// 找出长度最长的单词和重复次数最多的单词
void findLongestAndMostFrequentWord(const std::string& input) {
    std::string longestWord;
    std::string mostFrequentWord;
    std::vector<std::string> words;

    std::string word;
    for (int i = 0; i < input.length(); i++) {
        if (input[i] == ' ') {
            words.push_back(word);
            word = '';
        } else {
            word += input[i];
        }
    }
    words.push_back(word);

    int maxWordLength = 0;
    int maxWordFrequency = 0;
    for (const std::string& w : words) {
        if (w.length() > maxWordLength) {
            maxWordLength = w.length();
            longestWord = w;
        }
        int count = std::count(words.begin(), words.end(), w);
        if (count > maxWordFrequency) {
            maxWordFrequency = count;
            mostFrequentWord = w;
        }
    }

    std::cout << '最长的单词是:' << longestWord << std::endl;
    std::cout << '重复次数最多的单词是:' << mostFrequentWord << std::endl;
}

// 找出两个字符串之间的最大公共子序列
std::string findLongestCommonSubsequence(const std::string& s1, const std::string& s2) {
    int m = s1.length();
    int n = s2.length();
    std::vector<std::vector<int>> dp(m + 1, std::vector<int>(n + 1, 0));

    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (s1[i - 1] == s2[j - 1]) {
                dp[i][j] = dp[i - 1][j - 1] + 1;
            } else {
                dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]);
            }
        }
    }

    std::string result;
    int i = m, j = n;
    while (i > 0 && j > 0) {
        if (s1[i - 1] == s2[j - 1]) {
            result = s1[i - 1] + result;
            i--;
            j--;
        } else if (dp[i - 1][j] > dp[i][j - 1]) {
            i--;
        } else {
            j--;
        }
    }

    return result;
}

int main() {
    // 第一题
    std::cout << '请输入一个字符串: ';
    std::string input;
    std::getline(std::cin, input);
    std::string deletedString = deleteConsecutiveChars(input);
    std::cout << '删除连续出现次数超过2的字符后的字符串: ' << deletedString << std::endl;

    // 第二题
    std::cout << '请输入一个字符串: ';
    std::getline(std::cin, input);
    findLongestAndMostFrequentWord(input);

    // 第三题
    std::string s1, s2;
    std::cout << '请输入两个字符串,以空格分隔: ';
    std::cin >> s1 >> s2;
    std::string longestCommonSubsequence = findLongestCommonSubsequence(s1, s2);
    std::cout << '最大公共子序列: ' << longestCommonSubsequence << std::endl;

    return 0;
}

代码说明:

  • deleteConsecutiveChars 函数:

    • 循环遍历输入字符串,统计连续出现的字符数量。
    • 如果连续出现的次数大于 2,则跳过该字符,否则将其添加到结果字符串中。
  • findLongestAndMostFrequentWord 函数:

    • 将输入字符串按空格分割成单词,并存储到 words 向量中。
    • 循环遍历 words 向量,找出长度最长的单词和重复次数最多的单词。
  • findLongestCommonSubsequence 函数:

    • 使用动态规划的方法,创建 dp 表格,记录两个字符串所有子序列的长度。
    • 从 dp 表格中回溯,找到长度最大的公共子序列。

使用方法:

  1. 编译并运行程序。
  2. 按照提示输入字符串。
  3. 程序将输出相应的结果。

注意:

  • 在第一题中,我们使用了std::getline()函数来读取带有空格的输入字符串。
  • 在第二题中,我们使用了std::count()函数来计算单词的重复次数。
  • 在第三题中,我们使用了动态规划的方法来找出两个字符串之间的最大公共子序列。
C++ 字符串处理程序:删除连续字符、查找最长/最频繁单词、最大公共子序列

原文地址: https://www.cveoy.top/t/topic/pl7G 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录