关于c++:题解洛谷-P5734-深基6例6文字处理软件

0次阅读

共计 665 个字符,预计需要花费 2 分钟才能阅读完成。

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

思路

简略的字符串操作,其中字符串查找,s.find()函数,判断是否找到要用 if (s.find()!=string::npos) 来判断

代码

/*
 * @Description: 
 * @Author: 多多
 * @Date: 2020-10-25 08:40:40
 * @LastEditTime: 2020-10-25 09:00:40
 * @LastEditors: 多多
 */
#include <bits/stdc++.h>
using namespace std;

int main()
{freopen("./INPUT/P5734.in", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    string s = "";
    cin >> s;
    while (n--)
    {
        int x;
        cin >> x;
        if (x == 1)
        {
            string ss;
            cin >> ss;
            s += ss;
            cout << s << endl;
        }
        else if (x == 2)
        {
            int a, b;
            cin >> a >> b;
            s = s.substr(a, b);
            cout << s << endl;
        }
        else if (x == 3)
        {
            int a;
            string ss;
            cin >> a >> ss;
            s.insert(a, ss);
            cout << s << endl;
        }
        else if (x == 4)
        {
            string ss;
            cin >> ss;
            if (s.find(ss) != string::npos)
            {cout << s.find(ss) << endl;
            }
            else
            {cout << -1 << endl;}
        }
    }
    return 0;
}
正文完
 0