题意:
给一串字符,'['相当于home键(跳到最开始),']'相当于end(跳到最末尾)
问编辑出来的这一段文字是什么样;
思路:
用链表,碰到'['后,接下去的文字存入结构体,并且插入链表的开头,
碰到']'后,接下去文字存入结构体,插入链表的末尾;
结构储存,我用的是存所在位置的办法,这一个节点存的值是原串的哪个位置到哪个位置;
AC代码:
#include<cstdio>
#include<cstdlib>
#include<cstring>
const int N = 100000 + 10;
char str[N];
struct node {
int l,r;
node* next;
};
int main() {
while(gets(str)) {
int len = strlen(str);
char s[N];
int t = 0;
int cur = 1;
bool f = 0;
int i;
int pos = 0;
node* start = (node*)malloc(sizeof(node));
node* end;
for(i = 0; i < len; i++) {
if(str[i] == '[' || str[i] == ']') {
f = 1;
start -> next = NULL;
end = start;
start -> l = pos;
start -> r = i - 1;
pos = i + 1;
if(str[i] == '[')
cur = 1;
if(str[i] == ']')
cur = 0;
t = 0;
break;
}
}
if(!f) {
printf("%s\n",str);
continue;
}
i++;
for(; i < len; i++) {
if(str[i] == '[' || str[i] == ']') {
node* c = (node*)malloc(sizeof(node));
c -> l = pos;
c -> r = i - 1;
if(cur == 1) {
c -> next = start;
start = c;
}
else {
c->next = NULL;
end -> next = c;
end = c;
}
if(str[i] == '[')
cur = 1;
if(str[i] == ']')
cur = 0;
pos = i + 1;
continue;
}
if(i == len - 1) {
node* c = (node*)malloc(sizeof(node));
c -> l = pos;
c -> r = i;
if(cur == 1) {
c -> next = start;
start = c;
}
else {
c -> next = NULL;
end -> next = c;
end = c;
}
}
}
for(node* i = start; i != NULL; i = i->next) {
for(int j = i -> l; j <= i -> r;j++) {
printf("%c",str[j]);
}
}
printf("\n");
}
}