2 条题解

  • 0
    @ 2025-3-14 15:23:55
    #include <bits/stdc++.h>
    using namespace std;
    
    int main()
    {
        string str;
        getline(cin , str);
        //str.find("black") 返回 "black" 的起始位置
        //如果返回 npos,表示没有找到目标子字符串,
        //直接调用 replace 会导致未定义行为
        size_t pos = 0;//参数 pos 指定了从哪个位置开始查找。
        while ( (pos = str.find("black"),pos) != string::npos ) {
            str.replace(str.find("black") , 5 , "block");
        } 
        cout << str << endl;
        return 0;
    }
    
    • 0
      @ 2024-4-11 16:10:08
      #include<bits/stdc++.h>
      
      using namespace std;
      
      int main()
      {
      	string s;
      	cin>>s;
      	string a = "black",b = "block";
      	int p = s.find(a);
      	while(p != string::npos)
      	{
      		/*s.erase(p,5);
      		s.insert(p,b);
      		*/
      		s.replace(p,5,b);
      		p = s.find(a);
      	}
      	cout<<s<<endl;
      	return 0;
      }
      
      • 1

      信息

      ID
      76
      时间
      1000ms
      内存
      256MiB
      难度
      3
      标签
      递交数
      404
      已通过
      214
      上传者