用stl::stack超时 #9
查看原帖
用stl::stack超时 #9
598179
wisdomman楼主2023/2/28 16:22
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<stack>
typedef long long ll;
using namespace std;
stack<char>S;
int main()
{
	string tem[11];
	char c;
	while ((c = getchar()) && c != '\n')
		S.push(c);
	int cnt = 0;
	while (!S.empty())
	{
		char ch = S.top();
		S.pop();
		if (ch == ']')
			cnt++;
		else if (ch == '[')
		{
			int times = 0;
			if (tem[cnt][tem[cnt].length() - 1] >= '0' && tem[cnt][tem[cnt].length() - 1] <= '9')
			{
				times = tem[cnt][tem[cnt].length() - 1] - '0';
				tem[cnt].erase(tem[cnt].length() - 1, 1);
			}
			if (tem[cnt][tem[cnt].length() - 1] >= '0' && tem[cnt][tem[cnt].length() - 1] <= '9')
			{
				times = times * 10 + tem[cnt][tem[cnt].length() - 1] - '0';
				tem[cnt].erase(tem[cnt].length() - 1, 1);
			}
			for (int i = 0; i < times; i++)
			{
				tem[cnt - 1] += tem[cnt];
			}
			tem[cnt].clear();
			cnt--;
		}
		else
		{
			tem[cnt].append(1, ch);
		}
	}
	for (auto i = tem[0].rbegin(); i != tem[0].rend(); i++)
		cout << *i;;
	return 0;
}

使用stack(虽然可以不用)会导致#9超时,单纯改用数组就可以,想知道为什么

char s[20005];
int main()
{
    scanf("%s",s);
    string tem[11];
    int cnt=0;
    int slen=strlen(s);
    for(int i=slen-1;i>=0;i--)
    {
        char ch=s[i];
2023/2/28 16:22
加载中...