这个字符串的能否再进行优化?
查看原帖
这个字符串的能否再进行优化?
742571
deyerong楼主2022/9/25 20:37

代码如下,想试试字符串能不能解,最后还是TLE了 仨个点 请问同志们有优化方案吗。 比如再匹配方面上。(我太弱了QwQ)

#include <bits/stdc++.h>
using namespace std;
int n, m;
inline int read() {
	char c = getchar();
	int x = 0, f = 1;
	for (; !isdigit(c); c = getchar())
		if (c == '-') f = -1;
	for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
	return x * f;
}
inline void write(int x)
{
    if(x < 0){
    	putchar('-');
		x = -x;
	}
    if(x > 9) 
		write(x / 10);
    putchar(x % 10 + '0');
}
int main() {
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt","r",stdout);
	n = read(), m = read();
	string a(n, '0');
	int pd, x, y;
	while (m--) {
		// scanf("%d",&pd);
		pd = read();
		if (pd == 1) {
			x = read();
			string temp(x, '0'), oper(x, '1');
			int place = a.find(temp);
			if (place == -1) {place = 0;}
			else {
				a.replace(place, x, oper);
				place += 1;
			}
			write(place);
			putchar('\n');
		}
		else {
			x = read(), y = read();
			string temp(y, '0');
			a.replace(x - 1, y, temp);

		}
	}
	return 0;
}

以上,感谢ヾ(≧▽≦*)o

2022/9/25 20:37
加载中...