hdu 1274展开字符串(用栈来实现的)

2 篇文章 0 订阅

题目链接:【hdu 1274展开字符串】

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <stack>
using namespace std;
char str[300], si[2500000], sl[2500000];
int main()
{
	int n;
	scanf("%d", &n);
	while(n--)
	{
		stack<char>s;
		scanf("%s", str);
		int len=strlen(str);
		for(int i=0; i<len; i++)
		{
			if(str[i]==')')
			{
				int li=0;
				while(!s.empty())
				{
					char c=s.top();
					s.pop();
					if(c=='(') break;
					si[li++]=c;
				}
				int ci=0, di=1;
				while(!s.empty()&&s.top()>='0'&&s.top()<='9')
				{
					ci=ci+(s.top()-'0')*di;
					s.pop();
					di*=10;
				}
				if(ci==0) ci=1;
				while(ci--)
				{
					for(int j=li-1; j>=0; j--)
					{
						s.push(si[j]);
					}
				}
			}
			else 
			{
				if((str[i]>='0'&&str[i]<='9')||str[i]=='(') s.push(str[i]);
				else
				{
					if(s.empty()) s.push(str[i]);
					else if(s.top()>='0'&&s.top()<='9')
					{
						int d=0,li=1;
						while(!s.empty() && s.top()>='0' && s.top()<='9')
						{
							d = d+(s.top()-'0')*li;
							s.pop();
							li*=10;
						}
						while(d--)
						{
							s.push(str[i]);
						}
					}
					else s.push(str[i]);
				}
			}
		}
		int li=0;
		while(!s.empty())
		{
			si[li++]=s.top();
			s.pop();
		}
		memset(sl,'\0',sizeof(sl));
		int lk=0;
		for(int i=li-1; i>=0; i--)
		{
			sl[lk++]=si[i];
		}
		printf("%s\n", sl);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值