A
C
保
佑
\color{GREEN}A\space C\space保\space佑
A C 保 佑
算法思路
——怎么又是字符串?
——好吧,下次写DP了
显然又是字符串,先读入,再处理,分读到字母和方括号做不同判断。是方括号就找到配对的那一个(括号匹配问题,写个搜索),再分别对两括号之间的部分和之后的部分进行处理即可。
代码
#include<bits/stdc++.h>
using namespace std;
char a[30000];
int record;
int find(int n){//取数
int h=0;
while(a[n]>='0'&&a[n]<='9'){
h=(h<<3)+(h<<1)+a[n]-'0';
n++;
}
record=n;
return h;
}
void f(int x,int y){//模拟
if(x>y) return;
else if(a[x]>='A'&&a[x]<='Z'){
printf("%c",a[x]);
f(x+1,y);
}
else if(a[x]=='['){
int n=find(x+1);
int j=record;
int u=record;
int k=1;
while(k!=0){
if(a[j]=='[') k++;
else if(a[j]==']') k--;
j++;
}
for(int i=1;i<=n;i++){
f(u,j-1);
}
f(j,y);
}
}
int main(){
char c=getchar();
int i=0;
while((c<='Z'&&c>='A')||c=='['||c==']'||(c>='0'&&c<='9')){//读入
i++;
a[i]=c;
c=getchar();
}
f(1,i);
return 0;
}
可以做的优化但显然不需要了
可以在读入的时候就用数组记录下配对的括号,可以免去搜索的时间。