UVA 11988 - Broken Keyboard (a.k.a. Beiju Text)
Home键:光标移动到最前面,end移动到最后面。
用链表模拟过程就可以了。。。。。
用getchar会比scanf("%c“,&x)快
就不管内存泄露的问题啦~~嘻嘻
第一次看见陈锋的代码。好奇的交上去,被完虐。
我的0.426S他的0.099S
T T哭
#include<cstdio> struct link { link *next; char data; }; int main() { char temp; while(temp=getchar(),temp!=EOF) { link *head,*last,*p,*cur; last=head=new link; head->next=NULL; cur=last; while(temp!='\n') { if(temp=='[') { cur=head ; } else if(temp==']') { cur=last; } else { p=new link; p->data=temp; p->next=cur->next; cur->next=p; cur=cur->next; if(last->next!=NULL) last=cur; } temp=getchar(); } p=head->next; while(p!=NULL) { printf("%c",p->data); p=p->next; } printf("\n"); } }
陈锋的:
#include <cstring> #include <iostream> #include <cstdio> #include <cassert> #include <vector> using namespace std; struct ListNode { char c; ListNode* next; }; ListNode MEM[100000 + 10], *mem_next, *head, *tail, *insPos; int main() { while(true) { char c; mem_next = MEM + 1; head = MEM; head -> next = NULL; tail = head; insPos = tail; while(true) { c = getchar(); if(c == '\n') break; if(c == EOF) return 0; if(c == '[') insPos = head; else if(c == ']') insPos = tail; else { ListNode* newNode = mem_next++; newNode->c = c; newNode->next = insPos->next; insPos->next = newNode; insPos = newNode; if(tail->next != NULL) tail = insPos; } } ListNode* cur = head->next; while(cur != NULL) { putchar(cur->c); cur=cur->next; } puts(""); } return 0; }
新 blog : www.hrwhisper.me