本题有多种解法很有启发性
递归
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
char buf[100001];
void dfs(int l, int r)
{
int s = r;
while (s >= l && buf[s] != '[' && buf[s] != ']') s --;
if (buf[s] == ']') dfs(l, s-1);
for (int i = s+1 ; i <= r ; ++ i)
printf("%c",buf[i]);
if (buf[s] == '[') dfs(l, s-1);
}
int main()
{
while (gets(buf)) {
dfs(0, strlen(buf)-1);
printf("\n");
}
return 0;
}
deque双向队列
[cpp] view plaincopyprint?
#include <cmath>
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
#include <stack>
#include <deque>
using namespace std;
typedef long long LL;
#define EPS 10e-9
#define INF 0x3f3f3f3f
#define REP(i,n) for(int i=0; i<(n); i++)
char str[100010];
int main(){
while(scanf("%s",str)!=EOF){
deque<int > q;
int i=0;
while(str[i]=='['||str[i]==']') i++;
q.push_front(i);
while(str[i]){
if(str[i]=='['){
q.push_front(i+1);
str[i]='\0';
}
else if(str[i]==']'){
q.push_back(i+1);
str[i]='\0';
}
i++;
}
while(!q.empty()){
printf("%s",str+q.front());
q.pop_front();
}
printf("\n");
}
return 0;
}