虽然简洁度跟佳爷完全没法比,至少accepted很开心了
#include <iostream>
#include <string>using namespace std;const int maxd = 100003;char text[maxd];int mynext[maxd];void init(int len){ for (int i = 0;i < len;i++) mynext[i] = i+1; mynext[len] = -1;}int main(void){ //read string str; while (cin >> str) { //init next[] int len=0; while (str[len] == '['||str[len] == ']'&&str[len] == '\0')len++; str = str.substr(len); len = str.length(); init(len); //core int old_tail=0;//承接下文,初始化0处理[]happy类似情况 for (int i = 0;i < len;i++) { if (str[i] != '['&&str[i] != ']')old_tail = i + 1; else if (str[i] == '[' && (str[i + 1] != '[' && str[i+1] != ']'&& str[i+1]!='\0')) { mynext[old_tail] = -1; int old_head = mynext[0]; mynext[0] = i + 1 + 1;//str[i+1]为第一个输出字符 int j; for (j = i + 2;str[j] != '['&&str[j] != ']'&&str[j] != '\0';j++); mynext[j] = old_head;//str[old_head-1]输出的前一个字符为str[j-1] i = j-1;//跳过插进前面的字符 } else if(str[i]==']' && (str[i + 1] != '[' && str[i + 1] != ']' && str[i + 1] != '\0')) { mynext[old_tail] = i + 1 + 1; } } mynext[old_tail] = -1; //output for (int i = mynext[0];i != -1;i=mynext[i]) putchar(str[i-1]); putchar('\n'); } return 0;}