自测题设数据和第一个WA数据无误,但提交后结果是五个WA,希望能通过讨论解惑
查看原帖
自测题设数据和第一个WA数据无误,但提交后结果是五个WA,希望能通过讨论解惑
463509
ktw000111楼主2021/1/13 14:09
#include "stdafx.h"  
#include <iostream> 
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;

int main()
{
	int q,len,p,qtag=0;
	cin >> q;
	p = q;
	string str;
	string *result=new string[q];
	cin >> str;
	/*int test = 3,cc=0;
	while (test--) { cc++; cout << cc << endl; }*/
	do
	{
		string op;
		getline(cin, op, '\n');
		//getline(cin,op);
		//后接操作
		if (op[0] == '1')
		{
			string s1 = op.substr(2);
			//有不同的函数
			//result = str + s1;
			str = str.append(s1);
			result[qtag] = str;
			qtag++;
		}
		//截取操作
		else if (op[0] == '2')
		{
			char a, b;
			a = op[2]; b = op[4];
			int ai = a - '0', bi = b - '0';
			len = str.length();
			if (ai + bi > len)
				bi = len - ai;
			if(bi>=0)
				str = str.substr(ai, bi);
			result[qtag] = str;
			qtag++;
		}
		//插入操作
		else if (op[0] == '3')
		{
			char a = op[2];
			int ai = a - '0';
			string s1 = op.substr(4);
			str = str.insert(ai, s1);//参数ai为s1首字符插入str的位置,数组从0开始
			result[qtag] = str;
			qtag++;
		}
		//查子串操作
		else if (op[0] == '4')
		{
			int count = 0, begin = -1;
			string s1 = op.substr(2);
			//find函数用于寻找某个序列的在string中第一次出现的位置。
			//找不到子串会返回-1,不为0为True。0为False 
			//string::size_type pos = str.find(op);
			while ((begin = str.find(s1, begin + 1)) != string::npos)
			{
				count++;
			}
			if (count == 0)
			{
				result[qtag] = "-1";
				qtag++;
			}
			else
			{
				result[qtag] = to_string(count);
				qtag++;
			}
		}
		q--;
	} while (q >= 0);//getline首次循环读到回车,循环体未执行,修正
	for(int i=0;i<p;i++)
	{
		cout << result[i] <<endl;
	}
	system("pause");
	return 0;
}

//代码如上,IDE为VS2015

2021/1/13 14:09
加载中...