关于紫书(刘汝佳) 悲剧文本 UVA11988 的问题
AYTRW 2018-07-19 09:14:10 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
char text[100005];
struct node{
char data;
struct node *next;
};
struct node *p;
struct node *tail;
struct node *head;
int main(){
int i;
while(scanf("%s",text) != EOF){
head = (struct node*)malloc(sizeof(struct node));
head->next = NULL;
p = head;
tail = head;
for(i=0;i<=strlen(text)-1;i++){
if(text[i] == '['){
p = head;
}else if (text[i] == ']'){
p = tail;
}else{
struct node *n;
n = (struct node*)malloc(sizeof(struct node));
n->data = text[i];
n->next = p->next;
p->next = n;
p = n;
if(p->next == NULL) tail = p;
}
}
for(p=head->next;p!=NULL;p=p->next)
printf("%c",p->data);
printf("\n");
}
return 0;
}
自己写的代码,调试无问题,但放到洛谷OJ上就TLE,问问哪里出了问题
还有书上的做法是用数组来模拟指针,也不是很明白。。
补:
题目:
你在输入文章的时候,键盘上的Home键和End键出了问题,会不定时的按下。你却不知道此问题,而是专心致志地打稿子,甚至显示器都没开。当你打开显示器之后,展现你面前的数一段悲剧文本。你的任务是在显示器打开前计算出这段悲剧的文本。 给你一段按键的文
本,其中'['表示Home键,']'表示End键,输入结束标志是文件结束符(EOF)。
样例输入:
This_is_a_[Beiju]_text
[[]][][]Happy_Birthday_to_Tsinghua_University
输出
BeijuThis_is_a__text
Happy_Birthday_to_Tsinghua_University