解題思路:
f1:先對數字進行處理。然後判斷接下來的是( ,還是字母。若是(則遞歸調用f1,若是字母則直接輸出。
這道題傳說中可以用容器來做,但是後來才去的是以下做法:
/*
* 1274_5.cpp
*
* Created on: 2013年8月7日
* Author: Administrator
*
* 章澤天,我的女神!!!!
*/
#include <iostream>
using namespace std;
string str;
int fun(int index){
char c ;
int k;
int e = 0;
for(c = str[index++] ; index < str.length() && c != ')' ; c = str[index++]){
for( k = 0 ; isdigit(c) ; c = str[index++]){
k = 10*k + c - '0';
}
if(!k){
k = 1;
}
if( c == '('){
while(k--){
e = fun(index);
}
index = e;
}else{
while(k--){
cout<<c;
}
}
}
if( c == ')'){
return index;
}
}
int main(){
int t;
cin >> t;
while(t--){
cin >> str;
fun(0);
cout<<endl;
}
}
/*
* 1274_5.cpp
*
* Created on: 2013年8月7日
* Author: Administrator
*
* 章澤天,我的女神!!!!
*/
#include <iostream>
using namespace std;
string str;
int fun(int index){
char c ;
int k;
int e = 0;
for(c = str[index++] ; index < str.length() && c != ')' ; c = str[index++]){
for( k = 0 ; isdigit(c) ; c = str[index++]){
k = 10*k + c - '0';
}
if(!k){
k = 1;
}
if( c == '('){
while(k--){
e = fun(index);
}
index = e;
}else{
while(k--){
cout<<c;
}
}
}
if( c == ')'){
return index;
}
}
int main(){
int t;
cin >> t;
while(t--){
cin >> str;
fun(0);
cout<<endl;
}
}