6 条题解

  • 10
    @ 2021-10-16 21:16:09
    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    using namespace std;
    char s[1005],a[105],b[105];
    int main()
    {
    	cin.getline(s,1000);
    	cin >> a;
    	cin >> b;
    	int len = strlen(s);
    	int lena = strlen(a);
    	for(int i = 0 ; i < len ; i++)
    	{
    		if(i == 0 || s[i-1] == ' ')
    		{
    			int id1,id2;
    			id1 = i;
    			id2 = 0;
    			while(s[id1] == a[id2] && id2 < lena && id1 < len)
    			{
    				id1++,id2++;
    			}
    			if( id2 == lena && (s[id1] == ' ' || s[id1] == '\0') )
    			{
    				cout << b;
    				if(s[id1] != '\0')
    					cout << " ";
    				i = id1;
    				continue;
    			}
    			cout << s[i];
    			
    		}
    		else 
    		{
    			cout << s[i];
    		}
    	}
    	// cout << endl;
    }
    
    • 3
      @ 2025-5-9 18:09:01

      main函数部分如下:

      int main()
      {
      	getline(cin, str);
      	cin >> i >> j;
      	str =  " " + str + " ";
      	i =  " " + i + " ";
      	j =  " " + j + " ";
      	while(str.find(i, pos) != str.npos){
      		pos = str.find(i, pos);
      		str.replace(pos, i.size(), j);
      		pos ++;
      	}
      	str.erase(0, 1);
      	cout << str << endl;
      	return 0;
      }
      
      • 2
        @ 2025-3-21 20:12:49
        
        #include<bits/stdc++.h>
        using namespace std;
        const int N=1e5+10;
        const int INF=0x3f3f3f3f;
        int main(){
        	string s,a,b;
        	getline (cin,s);
        	cin>>a>>b;
        	s=" "+s+" ";
        	a=" "+a+" ";
        	b=" "+b+" ";
        	while(s.find(a)!=string::npos){
        		s.replace(s.find(a),a.size(),b);
        	}
        	for(int i=1;i<s.size()-1;i++){
        		cout<<s[i];
        	}
        }
        
        • 0
          @ 2026-7-12 15:31:17

          #include <bits/stdc++.h> using namespace std; //头文件 int main() { string s; string a,b; getline(cin,s); cin>>a>>b; s=" "+s+" "; a=" "+a+" "; b=" "+b+" "; int p=s.find(a); while(p!=-1){ s.replace(p,a.size(),b); p=s.find(a,p+1); } s.erase(0,1);//起点 长度 cout<<s; return 0; }

          • 0
            @ 2026-5-24 18:37:08

            #include<bits/stdc++.h> using namespace std; string a,b,c; int main(){ getline(cin,a); a +=" "; cin>>b>>c; int len= a.size(); string s=""; for (int i = 0 ;i<=len;i++){ if(a[i]!=' ') s+=a[i]; else{ if(s==b)cout<<c; else cout<<s; cout<<a[i]; s=""; } } return 0; }

            • -2
              @ 2024-5-12 20:18:57
              #include <iostream>
              #include<cstring>
              #include<bits/stdc++.h>
              using namespace std;
              char k[1145],a[114],b[114];
              int main()
              {
              	cin.getline(k,1000);
              	cin >> a;
              	cin >> b;
              	int len = strlen(k);
              	int lena = strlen(a);
              	for(int i = 0 ; i < len ; i++)
              	{
              		if(i == 0 || k[i-1] == ' ')
              		{
              			int id1,id2;
              			id1 = i;
              			id2 = 0;
              			while(k[id1] == a[id2] && id2 < lena && id1 < len)
              			{
              				id1++,id2++;
              			}
              			if( id2 == lena && (k[id1] == ' ' || k[id1] == '\0') )
              			{
              				cout << b;
              				if(k[id1] != '\0')
              					cout << " ";
              				i = id1;
              				continue;
              			}
              			cout << k[i];
              			
              		}
              		else 
              		{
              			cout << k[i];
              		}
              	}
              }
              
              • 1

              信息

              ID
              1098
              时间
              1000ms
              内存
              256MiB
              难度
              7
              标签
              递交数
              669
              已通过
              180
              上传者