SSU- 397 Text Editor

___Evan發表於2014-02-24

題目連結

簡單模擬題 連結串列還是用的不清楚


#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 1000005;
int pos,now;

struct Node
{
	char ch;
	int pre,next;
}node[maxn];
int main()
{
    //freopen("data.txt","r",stdin);  
	char ch;
	now = 0,pos = 0;
	node[0].ch = ' '; node[0].pre = -1; node[0].next = -1;
	while( 1 ){
		ch = getchar();
		if( ch == 'L' ){
			if( node[now].pre != -1 )
				now = node[now].pre;
		}
		else if( ch == 'R' ){
			if( node[now].next != -1 )
				now = node[now].next;
		}
		else if( ch >= 'a' && ch <= 'z' ){
			node[++pos].ch = ch;
			if( node[now].next != -1 )
				node[node[now].next].pre = pos;
			node[pos].next = node[now].next; node[now].next = pos;
			node[pos].pre = now; 
			now = pos;
		}
		else
			break;
	}
	now = node[0].next;
	while(now != -1 ){
		printf("%c",node[now].ch);
		now = node[now].next;
	}
	puts("");
    return 0;
}


相關文章