C++ 字符串处理:删除重复字符、查找最长单词和重复次数最多的单词、求最长公共子序列

本文提供了一个 C++ 程序,用于解决三个字符串处理问题:

  1. 删除连续出现超过两次的字符
  2. 查找一个字符串中最长的单词和重复次数最多的单词
  3. 找出两个字符串的最长公共子序列

代码实现

#include <iostream>
#include <string>
#include <vector>

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

// 找出最长的单词和重复次数最多的单词
void findLongestAndMostRepeatedWord(const std::string& input) {
    std::string longestWord;
    std::string mostRepeatedWord;
    int maxLength = 0;
    int maxCount = 0;
    
    std::string word;
    std::vector<std::string> words;
    for (int i = 0; i < input.length(); i++) {
        if (input[i] == ' ' || i == input.length() - 1) {
            if (i == input.length() - 1) {
                word += input[i];
            }
            words.push_back(word);
            word = '';
        } else {
            word += input[i];
        }
    }
    
    for (const std::string& w : words) {
        if (w.length() > maxLength) {
            maxLength = w.length();
            longestWord = w;
        }
        
        int count = 0;
        for (const std::string& w2 : words) {
            if (w == w2) {
                count++;
            }
        }
        
        if (count > maxCount) {
            maxCount = count;
            mostRepeatedWord = w;
        }
    }
    
    std::cout << '最长的单词:' << longestWord << std::endl;
    std::cout << '重复次数最多的单词:' << mostRepeatedWord << std::endl;
}

// 找出最长的公共子序列
std::string findLongestCommonSubsequence(const std::string& str1, const std::string& str2) {
    int m = str1.length();
    int n = str2.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 (str1[i - 1] == str2[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 (str1[i - 1] == str2[j - 1]) {
            result = str1[i - 1] + result;
            i--;
            j--;
        } else if (dp[i - 1][j] > dp[i][j - 1]) {
            i--;
        } else {
            j--;
        }
    }
    
    return result;
}

int main() {
    // 第一题
    std::string input1;
    std::cout << '请输入一个字符串(长度不超过100):';
    std::cin >> input1;
    std::string result1 = removeConsecutiveDuplicates(input1);
    std::cout << '删除连续超过2次的字符后的字符串:' << result1 << std::endl;
    
    // 第二题
    std::string input2;
    std::cout << '请输入一个字符串(长度不超过500,由单词构成,单词间由空格分割):';
    std::cin.ignore();
    std::getline(std::cin, input2);
    findLongestAndMostRepeatedWord(input2);
    
    // 第三题
    std::string input3_1, input3_2;
    std::cout << '请输入两个字符串(长度不一,共享一个公共子序列):' << std::endl;
    std::cout << '字符串1:';
    std::cin >> input3_1;
    std::cout << '字符串2:';
    std::cin >> input3_2;
    std::string result3 = findLongestCommonSubsequence(input3_1, input3_2);
    std::cout << '最长的公共子序列:' << result3 << std::endl;
    
    return 0;
}

代码说明

1. removeConsecutiveDuplicates 函数

该函数用于删除字符串中连续出现超过两次的字符。函数使用循环遍历字符串,记录每个字符的连续出现次数,如果次数超过 2,则不将其添加到结果字符串中。

2. findLongestAndMostRepeatedWord 函数

该函数用于查找字符串中最长的单词和重复次数最多的单词。函数首先将字符串分割成单词,然后分别计算每个单词的长度和出现次数,并记录最长的单词和出现次数最多的单词。

3. findLongestCommonSubsequence 函数

该函数用于找出两个字符串的最长公共子序列。函数使用动态规划算法,创建一个二维数组 dp 来记录每个位置的最长公共子序列长度。最后根据 dp 数组回溯得到最长公共子序列。

总结

本文提供了一个 C++ 程序,用于解决三个字符串处理问题。程序代码清晰易懂,并附带详细注释,适合学习 C++ 字符串处理的初学者。

C++ 字符串处理:删除重复字符、查找最长单词和重复次数最多的单词、求最长公共子序列

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

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