Luogu brush questions C++ language | P5734 word processing software

Learn C++ from a baby! Record the questions in the process of Luogu C++ learning and test preparation, and record every moment.

Attached is a summary post: Luogu Brush Questions C++ Language | Summary


【Description】

You need to develop a word processing software. Initially enter a string as the initial document. It can be considered that the beginning of the document is the 0th character. The following operations need to be supported:

  • 1 str: Followed by insert, insert the string strstr after the document, and output the string of the document.
  • 2 ab: Intercept the document part, keep only  b characters from the ath  character  in the document  , and output the document string.
  •  3 a str: Insert a fragment, insert the string str in front of the ath character in the document, and output the string of the document
  • 4 str: Find the substring, find the first position of the string str in the document and output it; if not found, output −1.

In order to simplify the problem, it is stipulated that the initial document and str in each operation do not contain spaces or newlines. There will be at most  q  operations.

【enter】

The first line inputs a positive integer  q , indicating the number of operations.

The second line enters a string str, which represents the initial string.

Starting from the third line, go down  q  lines, each line represents an operation, and the operation is as shown in the title description.

【Output】

A total of  n  lines are output.

For each operation 1,2,3, output a string as required by the operation.

For operation 4, output an integer as required by the operation.

【Input example】

4 ILove 1 Luogu 2 5 5 3 3 guGugu 4 gu

【Example of output】

ILoveLuogu Luogu LuoguGugugu 3

【Code Explanation】

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

int main()
{
    int flag, a, b, n;
    string s, str;
    cin >> n >> s;
    for (int i=1; i<=n; i++) {
        cin >> flag;
        if (flag==1) {
            cin >> str;
            s = s + str;
            cout << s << endl;
        } else if (flag==2) {
            cin >> a >> b;
            s = s.substr(a, b);
            cout << s << endl;
        } else if (flag==3) {
            cin >> a >> str;
            s = s.insert(a, str);
            cout << s << endl;
        } else {
            cin >> str;
            a = s.find(str);
            cout << a << endl;
        }
    
    }
    return 0;
}

【operation result】

4
ILove
1 Luogu
ILoveLuogu
2 5 5
Luogu
3 3 guGugu
LuoguGugugu
4 gu
3

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/132710763