您的位置:首页 > 其它

hdu 1274 展开字符串

2013-02-26 17:05 337 查看
题目链接:

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

一开始solve没有返回位置,然后递归的时候就出错了。。。

STL特别好用。。。

View Code

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

int  solve(string &str,string &restr,int pos){
int len=str.length();
while(pos<len){
//是字母
if(str[pos]>='a'&&str[pos]<='z'){
restr.push_back(str[pos]);
}else if(str[pos]>='0'&&str[pos]<='9'){                        //是数字
int count=0;
while(pos<len&&str[pos]>='0'&&str[pos]<='9'){
count=10*count+str[pos]-'0';
pos++;
}
//后面是字母
if(str[pos]>='a'&&str[pos]<='z'){
for(int i=1;i<=count;i++){
restr.push_back(str[pos]);
}
}else if(str[pos]=='('){                     //后面是括号
string temp;
pos=solve(str,temp,pos+1);
for(int i=1;i<=count;i++){
restr.append(temp);  //追加
}
}
}else if(str[pos]=='('){          //是括号
string temp;
pos=solve(str,temp,pos+1);
restr.append(temp);
}else if(str[pos]==')'){
break;
}
pos++;
}
return pos;
}

int main(){
int n;
scanf("%d",&n);
while(n--){
string str,restr;
cin>>str;
solve(str,restr,0);
cout<<restr<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: