牛客练习赛31: D. 神器大师泰兹瑞与威穆(链表)

版权声明:本文为博主原创文章,你们可以随便转载 https://blog.csdn.net/Jaihk662/article/details/84146383

链接:https://ac.nowcoder.com/acm/contest/218/D
来源:牛客网
 

题目描述

「只要我拉动绳线,你就得随之起舞。」          ——泰兹瑞

       泰兹瑞来到卡拉德许之后,由于他精湛的神器制造技术,可谓是过的如鱼得水。这次,他为自己打造了一个编辑器,称为威穆(Veim)。操作威穆时,有两种模式,具体操作如下。

Normal Mode

- 按下 i :进入 Insert Mode
- 按下 f :紧接着一个小写字母 char,若当前光标后(右)方有至少一个 char ,将光标移动到其所在位置,否则不移动。
- 按下 x :删除当前光标所在位的字符,后面的字符均会前移一格。
- 按下 h :将光标向左(前)移动一格,若无法移动就不移动。
- 按下 l :将光标向右(后)移动一格,若无法移动就不移动。
- 若按下了其他字符:无任何效果。

Insert Mode

- 按下非 e 小写字母 char :在光标当前位置前插入这个字母 char。
- 按下 e :退出 Insert Mode(进入 Normal Mode)。 

       (具体请见样例)

       现在泰兹瑞的威穆中已经写入了一个字符串 s 。接下去泰兹瑞进行了一波操作(按下了若干按键),他的按键序列为 t 。现给出 s 和 t ,求这波操作之后威穆内留下的字符串。

输入描述:

两行,第一行字符串 s ,第二行字符串 t 。

输出描述:

一行,威穆里最后留下的字符串。

输入

applese
xfllhlia

输出

pplaese

手写个链表按题意模拟就好了

每个节点存有当前字符ch,以及R[]表示所有字符下一个出现的位置节点,如果没有就指向尾巴

其中R[]可以像线段树那样懒惰更新

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<string>
#include<math.h>
#include<queue>
#include<list>
#include<stack>
#include<iostream>
using namespace std;
#define LL long long
#define mod 1000000007
typedef struct Node
{
	char ch;
	Node *next, *last, *R[30];
}Node;
Node *head, *tail, *now;
char s1[100005], s2[100005];
void Insert(Node *p, char ch)
{
	int i;
	Node *temp;
	temp = new Node;
	temp->ch = ch;
	temp->next = p;
	temp->last = p->last;
	for(i=0;i<=26;i++)
	{
		temp->R[i] = (p->last)->R[i];
		if(ch-'a'==i)
			(p->last)->R[i] = temp;
	}
	(p->last)->next = temp;
	p->last = temp;
}
void Erase(Node *p)
{
	int ch;
	ch = p->ch-'a';
	(p->last)->R[ch] = p->R[ch];
	(p->last)->next = p->next;
	(p->next)->last = p->last;
	delete(p);
}
Node* ToRight(Node *p, char ch)
{
	if(p->R[ch-'a']!=tail)
		return p->R[ch-'a'];
	else
		return p;
}
void Print(Node *p)
{
	while(p!=tail)
	{
		p = p->next;
		printf("%c", p->ch);
	}
	printf("\n");
}
int main(void)
{
	char ch;
	int n, m, i, j, op;
	scanf("%s%s", s1+1, s2+1);
	n = strlen(s1+1);
	m = strlen(s2+1);
	tail = new Node, head = new Node;
	head->next = tail, tail->last = head;
	for(i=0;i<=26;i++)
		head->R[i] = tail;
	now = tail;
	for(i=1;i<=n;i++)
		Insert(now, s1[i]);
	while(now->last!=head)
	{
		if(now!=tail)
		{
			for(j=0;j<=26;j++)
			{
				(now->last)->R[j] = now->R[j];
				if(now->ch-'a'==j)
					(now->last)->R[j] = now;
			}
		}
		now = now->last;
	}
	op = 1;
	for(i=1;i<=m;i++)
	{
		//Print(head);
		ch = s2[i];
		if(op==1)
		{
			if(ch=='i')
				op = 2;
			else if(ch=='f')
			{
				ch = s2[++i];
				now = ToRight(now, ch);
			}
			else if(ch=='x')
			{
				now = now->next;
				Erase(now->last);
			}
			else if(ch=='h')
			{
				if(now!=tail)
				{
					for(j=0;j<=26;j++)
					{
						(now->last)->R[j] = now->R[j];
						if(now->ch-'a'==j)
							(now->last)->R[j] = now;
					}
				}
				if(now->last!=head)
					now = now->last;
			}
			else if(ch=='l')
			{
				if(now->next!=tail)
					now = now->next;
			}
		}
		else
		{
			if(ch=='e')
				op = 1;
			else
				Insert(now, ch);
		}
	}
	Print(head);
	return 0;
}
/*
aaaabaaaab
fbhhhhfbxhhhhfbikehhhhhhhhhhhhhhbkx
*/

猜你喜欢

转载自blog.csdn.net/Jaihk662/article/details/84146383