Luo Gu:P1928エイリアンコード(バックトラッキング)

トピック:

ここに画像の説明を挿入

分析:Leetcodeは以前にそれを行いました。コードの品質を改善する必要があります。

コード:

#include<bits/stdc++.h>
using namespace std;
int x;
string s;
string f()
{
    
    //[ ] 中的第一个字母是是数字 
 int c=s[x]-'0';
 x++;
 if(s[x]>='0'&&s[x]<='9') {
    
    
  c=c*10;
  c=c+s[x]-'0';
  x++;
 }
 string ans;
 for(;x<s.length();x++)
 {
    
    
  if(s[x]=='[') {
    
    
   x++;
   ans+=f();
  }
  if(s[x]==']') {
    
    
   x++;
   string st;
   for(int i=0;i<c;i++) st+=ans;
   //cout<<ans<<"-------";
   return st;
  }
  ans+=s[x];
 }
}
int main()
{
    
    
 cin>>s;
 x=0;
 for(;x<s.length();)
 {
    
    
  if(s[x]=='[') {
    
    
   x++; cout<<f();  
  }
  else 
  {
    
    
   cout<<s[x]; x++;
  }
 }
}

おすすめ

転載: blog.csdn.net/weixin_42721412/article/details/108488826