HDU 1274 展开字符串

题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1274

 

一个很不错的递归题,我主要的过程就如一般的表达式求解,遇到左括号进入下层递归否则一直按每个字母前面的数字决定其输出的次数,括号也是如此,而递归结束的标志/则是遇到 右括号或者到字符串的结尾,注意每次递归结束需要返回下次遍历的序号

 

代码如下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
using namespace std;

/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
*/

int len;
char a[255];

int xiaohao(int k)
{
    int i,e;
    char c;
    for(c=a[k++];k<len&&c!=')';c=a[k++])
    {
        for(i=0;isdigit(c);c=a[k++])
            i=i*10+c-'0';
        if(i==0)
            i=1;
        if(c=='(')
        {
            while(i--)
                e=xiaohao(k);
            k=e;
        }
        else
        {
            while(i--)
                putchar(c);
        }
    }
    if(c==')')
        return k;
}

int main()
{
    int t,i;
    cin>>t;
    while(t--)
    {
        scanf("%s",a);
        len=strlen(a);
        xiaohao(0);
        cout<<endl;
    }
    return 520;
}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值