求助!40分

P5734 【深基6.例6】文字处理软件

zhuzihan0903 @ 2024-09-08 17:23:11

#include<iostream>
#include<string>
using namespace std;
int main(){
    int n,opt,l,r;
    string s,a;
    cin>>n;
    cin>>s;
    while(n--){
        cin>>opt;
        if(opt==1){
            cin>>a;
            s.append(a);
            cout<<s<<endl;
        }else if(opt==2){
            cin>>l>>r;
            s=s.substr(l,r);
            cout<<s<<endl;
        }else if(opt==3){
            cin>>l>>r;
            s.insert(l,a);
            cout<<s<<endl;
        }
        else{
            cin>>a;
            cout<<(int)s.find(a)<<endl;
        }

    }
    return 0;
}

by bluedemmo @ 2024-09-08 17:25:39

@zhuzihan0903

#include<bits/stdc++.h>
using namespace std;

int n;

int x;

int a,b;

string str;
string s;

int main(){

    cin>>n;
    cin>>s;

    for(int k=1;k<=n;k++)
    {
        cin>>x;

        if(x==1)
        {
            cin>>str;
            s=s+str;
            cout<<s<<"\n";
        }
        else if(x==2)
        {
            cin>>a>>b;
            string tmp="";
            for(int i=a;i<b+a;i++)
            {
                tmp+=s[i];
            }
            s=tmp;
            cout<<s<<"\n";
        }
        else if(x==3)
        {
            cin>>a;
            cin>>str;
            string s2="",s3="";
            for(int i=0;i<a;i++)
            {
                s2+=s[i];
            }
            for(int i=a;i<s.length();i++)
            {
                s3+=s[i];
            }
            s=s2+str+s3;
            cout<<s<<"\n";

        }
        else if(x==4)
        {
            cin>>str;
            bool f=1;
            for(int i=0;i<s.length();i++)
            {
//              cout<<s[i]<<"|"<<str[0]<<" ";
                if(s[i]==str[0])
                {
                    string s4="";
                    for(int j=i;j<i+str.length();j++)
                    {
                        s4+=s[j];
                    }
//                    cout<<"*"<<s4;
                    if(s4==str)
                    {
                        f=0;
                        cout<<i<<"\n";
                        break;
                    }
                }
            }

            if(f)cout<<-1<<"\n";

        }

    }

    return 0;
}

回关?


by 2023iostream @ 2024-09-22 12:47:18

#include <bits/stdc++.h>
int n;
std::string s;
int main(){
    std::cin >> n >> s;
    while(n -- ){
        int x;
        std::cin >> x;
        if(x == 1){
                std::string c;
                std::cin >> c;
                s = s + c;
                std::cout << s << "\n";
        }else if(x == 2){
            int a, b;
            std::cin >> a >> b;
            s = s.substr(a, b);
            std::cout << s << "\n";
        }else if(x == 3){
            int a;
            std::string c;
            std::cin >> a >> c;
            s.insert(a, c);
            std::cout << s << "\n";
        }else{
            std::string c;
            std::cin >> c;
            if(s.find(c) < s.size()) std::cout << s.find(c) << "\n";
            else std::cout << -1 << "\n";
        }
    }
 } 

by zhuzihan0903 @ 2024-11-17 14:26:54

@bluedemmo@2023iostream 感谢!!!


|