2 条题解

  • 0
    @ 2025-10-8 17:12:02
    #include <iostream>
    #include <string>
    #include <algorithm>
    using namespace std;
    
    // 替换函数,忽略大小写
    void replace_word(string& str, const string& old_word, const string& new_word) {
        size_t pos = 0;
        while ((pos = str.find(old_word, pos)) != string::npos) {
            str.replace(pos, old_word.length(), new_word);
            pos += new_word.length();  // 防止替换后继续替换同一位置
        }
    }
    
    int main() {
        string input, output;
        string line;
    
        // 逐行读取输入
        while (getline(cin, line)) {
            input += line + "\n";  // 逐行拼接成一个完整字符串
        }
    
        output = input;
    
        // 临时替换成占位符
        replace_word(output, "sky", "[sky_520000]");
        replace_word(output, "Sky", "[Sky_520000]");
        replace_word(output, "SKY", "[SKY_520000]");
        replace_word(output, "aqua", "[aqua_520000]");
        replace_word(output, "Aqua", "[Aqua_520000]");
        replace_word(output, "AQUA", "[AQUA_520000]");
    
        // 替换占位符为目标单词
        replace_word(output, "[sky_520000]", "aqua");
        replace_word(output, "[Sky_520000]", "Aqua");
        replace_word(output, "[SKY_520000]", "AQUA");
        replace_word(output, "[aqua_520000]", "sky");
        replace_word(output, "[Aqua_520000]", "Sky");
        replace_word(output, "[AQUA_520000]", "SKY");
    
        // 输出替换后的结果
        cout << output;
    
        return 0;
    }
    
    • 0
      @ 2025-10-8 17:11:54
      #include <iostream>
      #include <string>
      #include <algorithm>
      using namespace std;
      
      // 替换函数,忽略大小写
      void replace_word(string& str, const string& old_word, const string& new_word) {
          size_t pos = 0;
          while ((pos = str.find(old_word, pos)) != string::npos) {
              str.replace(pos, old_word.length(), new_word);
              pos += new_word.length();  // 防止替换后继续替换同一位置
          }
      }
      
      int main() {
          string input, output;
          string line;
      
          // 逐行读取输入
          while (getline(cin, line)) {
              input += line + "\n";  // 逐行拼接成一个完整字符串
          }
      
          output = input;
      
          // 临时替换成占位符
          replace_word(output, "sky", "[sky_520000]");
          replace_word(output, "Sky", "[Sky_520000]");
          replace_word(output, "SKY", "[SKY_520000]");
          replace_word(output, "aqua", "[aqua_520000]");
          replace_word(output, "Aqua", "[Aqua_520000]");
          replace_word(output, "AQUA", "[AQUA_520000]");
      
          // 替换占位符为目标单词
          replace_word(output, "[sky_520000]", "aqua");
          replace_word(output, "[Sky_520000]", "Aqua");
          replace_word(output, "[SKY_520000]", "AQUA");
          replace_word(output, "[aqua_520000]", "sky");
          replace_word(output, "[Aqua_520000]", "Sky");
          replace_word(output, "[AQUA_520000]", "SKY");
      
          // 输出替换后的结果
          cout << output;
      
          return 0;
      }
      
      • 1

      *【字符串:find 和 replace】交换

      信息

      ID
      6670
      时间
      520ms
      内存
      240MiB
      难度
      10
      标签
      递交数
      11
      已通过
      2
      上传者