ZOJ 2571 Big String Outspread

不太会用递归的说。。不过一把过了,纪念一下。。代码太水,欢迎批评指正哭

#include <iostream>
#include <string>
using namespace std;


bool isNum(char ch)
{
	return (ch <= '9'&&ch>='0');
}
bool isLetter(char ch)
{
	return (ch <= 'z'&&ch>='a');
}

string outSpread(string str)
{
	string rslt = "";
	int i = 0 ;
	
	
	while ( i < str.size() )
	{
		int tmp = 0 ;
		if ( isNum(str[i]) )
		{
			//get the counts of next string
			tmp += str[i]-'0';
			while( i<str.size()-1 && isNum((str[++i])) )
				tmp = tmp*10 + str[i]-'0';

			if( i<str.size() && isLetter(str[i]) )
			{
				for(int j=0; j<tmp; j++)
					rslt += str[i]; 
				i++;
			}
			else if( i<str.size() && str[i]=='(' )
			{
				i++;
				int match = 1;
				string rec = "";
				while( i<str.size())
				{
					if(str[i]=='(')match++;
					else if(str[i]==')')
					{
						match -- ;
						if(match==0)
						{
							i++;
							break;
						}
					}
					rec += str[i];
					i++;
				}
				rec = outSpread(rec);
				for(int j=0; j<tmp; j++)
					rslt += rec;
			}


		}
		else if(isLetter(str[i]))
		{
			rslt += str[i];
			i++;
		}
		else if(str[i]=='(')
		{
			i++;
			int match = 1;
			string rec = "";
			while( i<str.size())
			{
				if(str[i]=='(')match++;
				else if(str[i]==')')
				{
					match -- ;
					if(match==0)
					{
						i++;
						break;
					}
				}
				rec += str[i];
				i++;
			}
			rec = outSpread(rec);
			for(int j=0; j<tmp; j++)
				rslt += rec;
		}

	}
	return rslt;
}


int main()
{
	int num;
	cin >> num;
	string tmp;
	while(num--)
	{
		cin >> tmp;
		cout << outSpread(tmp)<<endl;
	}
	return 0;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值