|
- #include<iostream>
- using namespace std;
- string s;
- int l,sum,i,j,p[100];///sum个正括号,分别在p[i]
- int num(int x)
- {
- int nn;
- if (s[x+2]>='0' && s[x+2]<='9')
- nn=(s[x+1]-'0')*10+(s[x+2]-'0');
- else
- nn=(s[x+1]-'0');
- return nn;
- }
- string be(int x)
- {
- string bes="";
- for (j=0;j<=x-1;j++) bes=bes+s[j];
- return bes;
- }
- string he(int l,int r,int c)///左端点 右端点 重复次数
- {
- string her="",sher;///sher记录her
- if (c<=9) l=l+2;
- else l=l+3;
- r--;
- for (j=l;j<=r;j++) her=her+s[j];
- sher=her;
- for (j=1;j<=c-1;j++) her=her+sher;
- return her;
- }
- string af(int l,int r)
- {
- string aft="";
- for (j=l+1;j<=r;j++) aft=aft+s[j];
- return aft;
- }
- int main()
- {
- cin>>s;
- l=s.size(),sum=0;///sum括号的个数
- for (i=0;i<=l-1;i++)
- {
- if (s[i]=='[') sum++,p[sum]=i;
- }
- for (i=sum;i>=1;i--)///从内向外扫
- {
- l=s.size();
- int number=num(p[i]);///重复次数
- string before=be(p[i]);///把该括号之前的串复制下来
- int pp=p[i];
- while (s[pp]!=']') pp++;///反括号在s[pp]
- string here=he(p[i],pp,number);///在[]中的字符串
- string after=af(pp,l);///在反括号之后的串
- s=before+here+after;
- }
- cout<<s;
- return 0;
- }
复制代码 |
|