题解 P1928 【外星密码】

z3475

2017-11-10 21:49:24

Solution

直说思路,利用C++里的函数递归解决问题,自行重写了读入/读出 如 AfF[4RA[2A]] 可以当成 AfF+[4RA+[2A]] 递归处理[2A] AfF+[4RAAA] 递归处理[4RAAA] AfFRAAARAAARAAA 上代码 ```cpp #include<bits/stdc++.h> #define ri register int using namespace std; string a; int post=0; char getchar2(){return a[post++];} //自行定义getchar int gi(){ //自定义读入整数,注意读完post(位置)减一确保在数字后一位 int u=0;char p=getchar2(); while (!(p>='0'&&p<='9')) p=getchar2(); while ((p>='0'&&p<='9')) u=u*10+p-'0',p=getchar2(); post--; return u; } string po(int u){//递归函数 char p=getchar2(); string yy; while (p!=10){ if (p=='['){ int l=gi(); yy.append(po(l)); }else if (p==']'){ string o; for (int i=1;i<=u;i++) o.append(yy); return o; }else yy.push_back(p); p=getchar2(); } return yy; } int main() { cin >> a; //开局读入一行 a.push_back(10); //加入回车符表结束 cout << po(1); //调用递归函数 } ```