hdu 1274 展开字符串 (简单dfs)

http://acm.hdu.edu.cn/showproblem.php?pid=1274

 

思路: 找到匹配的区间 之后dfs

 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
char str[300];
void dfs(int l,int r)
{
    int i,j,k;
    int tl,tr,coun;
    for(i=l;i<=r;i++)
    {
        if(str[i]<='9'&&str[i]>='0')
        {
            coun=str[i]-'0';
            tr=tl=i+1;
            if(str[tl]=='(')
            {
              int tcnt=1;
              for(j=tl+1;j<=r;j++)
              {
                if(str[j]=='(') tcnt++;
                else if(str[j]==')') tcnt--;
                if(tcnt==0) {tr=j;break;}
              }
            }
            for(j=1;j<=coun;j++)
            {
                dfs(tl,tr);
                i=tr; continue;
            }
        }
        else if(str[i]<='z'&&str[i]>='a')
        {
            printf("%c",str[i]);
        }
    }
}
int main()
{
    int n;
    int i,j,k;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s",str);
        int len=strlen(str);
        dfs(0,len-1);
        printf("\n");
    }
    return 0;
}
View Code

 

posted @ 2015-01-22 15:55  sola94  阅读(147)  评论(0编辑  收藏  举报