HDU-1274-展开字符串(dfs分段字符串处理)

//传送门:http: //acm.hdu.edu.cn/showproblem.php?pid=1274
#include <queue>
#include <functional>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <assert.h>
using namespace std;
#define N 15 
string s;
int dfs(int p,int len)        // P表示当前位置 ,len表示字符串长度 
{                             // 返回的值为一个括号内的 字符串长度-2 
	int num = 0;              // 当前数字的值 
	for(int i=p;i<len;i++){   // 从当前位置遍历到 len-1 ,遇到 ')' 跳出 

	if(s[i]>='a'&&s[i]<='z'){ // 如果当前字符为 字母 
    int tmp =max(num,1);      // num 至少为1 
    for(int j=0;j<tmp;j++) printf("%c",s[i]);  //输出 num遍 
    num = 0;                  // 要重置为 0  
	}else if(s[i]>='0'&&s[i]<='9'){   // 如果当前字符为 数字 
	 num = num * 10 + s[i]-'0';        
	}else if(s[i]=='('){               
	int tmp = max(num,1);             // 递归下探 tmp 遍 
	for(int j=0;j<tmp-1;j++) dfs(i+1,len);  
	i=i+dfs(i+1,len)+1;               // 跳过 ("s") 的长度  
	num=0;          
	}else return i-p;                 // 果当前字符为 ')' 跳出	
		
	}	
}
int main()
{
  int t;
  scanf("%d",&t);
  while(t--){
  cin>>s;
  dfs(0,s.length());
  puts("");
  } 
  return 0;
} 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值