Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2123 Accepted Submission(s): 1019
题解:没写出来,看了人家的修改了自己的,然而wawawa;
我的:
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #include<queue> #include<stack> #include<vector> #include<map> #include<string> using namespace std; const int INF=0x3f3f3f3f; const double PI=acos(-1.0); #define mem(x,y) memset(x,y,sizeof(x)) #define SI(x) scanf("%d",&x) #define SL(x) scanf("%lld",&x) #define PI(x) printf("%d",x) #define PL(x) printf("%lld",x) #define P_ printf(" ") #define T_T while(T--) typedef long long LL; const int MAXN=250010; char s[MAXN]; int k; int len; //int i; int dfs(int x){ int temp,e; for(int i=x;i<len;i++){ //temp=1; if(s[i]==‘)‘)return i+1; /*if(s[i]>=‘0‘&&s[i]<=‘9‘){ temp=s[i]-‘0‘; i++; while(s[i]>=‘0‘&&s[i]<=‘9‘){ temp=temp*10+s[i]-‘0‘;i++; } }*/ for(temp=0;isdigit(s[i]);i++) temp=temp*10+s[i]-‘0‘; if(!temp) temp=1; if(s[i]==‘(‘){ while(temp--){ e=dfs(i+1); } i=e; } else{ while(temp--)printf("%c",s[i]); } } } int main(){ int T; SI(T); T_T{ memset(s,0,sizeof(s)); scanf("%s",s); len=strlen(s); dfs(0);puts(""); } return 0; }
大神ac;
#include <iostream> #include <cctype> #include <cstring> #include <string> using namespace std; string s; int fun(int ith) { int k,e; char c; for(c=s[ith++];ith<s.size()&&c!=‘)‘;c=s[ith++])//递归结束的条件是字符串结束或遇到右括号 { for(k=0;isdigit(c);c=s[ith++]) k=k*10+c-‘0‘; if(!k) k=1; if(c==‘(‘){ while(k--) e=fun(ith); ith=e;//重置ith的值,到下层递归结束的位置 } else { while(k--) putchar(c); } } if(c==‘)‘) return ith;//返回本次读到结尾的位置 } int main() { int i,j,k,T; cin>>T; while(T--) { s.clear(); cin>>s; fun(0);//进入递归 cout<<endl; } return 0; }
原文:http://www.cnblogs.com/handsomecui/p/5087251.html