链表之破损的键盘

uva编号:11988

问题描述:

You’re typing a long text with a broken keyboard. Well it’s not so badly broken. The only problem
with the keyboard is that sometimes the “home” key or the “end” key gets automatically pressed
(internally).
You’re not aware of this issue, since you’re focusing on the text and did not even turn on the
monitor! After you finished typing, you can see a text on the screen (if you turn on the monitor).
In Chinese, we can call it Beiju. Your task is to find the Beiju text.

你有一个破损的键盘。键盘上的所有键都可以正常工作,但有时Home键或者End键会自
动按下。你并不知道键盘存在这一问题,而是专心地打稿子,甚至连显示器都没打开。当你
打开显示器之后,展现在你面前的是一段悲剧的文本。你的任务是在打开显示器之前计算出
这段悲剧文本。
Input

There are several test cases. Each test case is a single line containing at least one and at most 100,000 letters, underscores and two special characters ‘[’ and ‘]’. ‘[’ means the “Home” key is pressed internally, and ‘]’ means the “End” key is pressed internally. The input is terminated by end-of-file(EOF).

Output

For each case, print the Beiju text on the screen.

输入包含多组数据。每组数据占一行,包含不超过100000个字母、下划线、字符“[”或
者“]”。其中字符“[”表示Home键,“]”表示End键。输入结束标志为文件结束符(EOF)。输
入文件不超过5MB。对于每组数据,输出一行,即屏幕上的悲剧文本。

Sample Input

This_is_a_[Beiju]_text
[[]][][]Happy_Birthday_to_Tsinghua_University

Sample Output

BeijuThis_is_a__text
Happy_Birthday_to_Tsinghua_University

我们先来说一个比较耗费时间的一个方法,也就是用数组来解决这个问题,说一下数组解决思路:

用一个数组来存放输入的数据,另外一个数组来存放应当正确存放的数据。

然后遇到 [ ,就连续把数据进行头插,直到遇到 ], 然后把游标变到新数组的尾部,当然了,中间遇到非 [ 或者非 ] 的数据直接在头部按照顺序插入即可。

这个要在新数组中插入一个数据呢,时间开销比较大,因为这里就涉及到了大量的数据移动,不可取

话不多说,直接上代码

uva11988.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100005 


int main()
{
	char old_str[MAX_SIZE];
	char new_str[150005];//主要是涉及到了数据的移动
	memset(new_str,'\0',sizeof(new_str));
	while(scanf("%s",old_str) != EOF) {
		int old_len = strlen(old_str);
		int index = 0;
		for(int i = 0;i < old_len;i++) {
			if(old_str[i] != '[' && old_str[i] != ']') {
				new_str[index++] = old_str[i];
			} else if(old_str[i] == '[') {
				//从尾部开始移动数据
				//空出来
				//计算空出多少个位置
				int move_count = 0;
				int cursor = i++;
				while(old_str[cursor] != '\0' && old_str[cursor] != ']') {
					if(old_str[cursor] != '[') {
						move_count++;
					}
					cursor++;
				}
				if(move_count > 0) {
					//开始移动了
					int new_change_len = strlen(new_str);
					for(int j = new_change_len;j > 0;j--) {
						new_str[j+move_count-1] = new_str[j-1];
					}
					//放值
					for(int n = 0;n < move_count;n++) {
						if(old_str[i] == '[') {
							i++;//往前走一个
						}
						new_str[n] = old_str[i++];
					}
					//index肯定会改变
					index += move_count;
				}
			} 
		}
		new_str[index] = '\0';//这里加上'\0'解决
		printf("%s",new_str);
	}
	return 0;
}

运行结果:

如果把这个代码往uva提交

 

会直接报一个Runtime error的错误 

上面我们就会有很明显的时间开销,下面我们尝试用单链表来解决问题

之前我把单链表做成了一个动态库,因为现在打印数据的格式不同,我又在单链表里面加了一个函数

下面这个函数是我重新加入的,那么我们就必须重新生成动态库,才能用到里面的方法

然后我们来看一下单链表的设计思想:

好了,话不多说,直接上代码:

uva11988_1.c 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linklist.h"
#define MAX_SIZE 100005

int main()
{
	//还是要来输入数据
	char old_str[MAX_SIZE];
	//创建单链表
	while(scanf("%s",old_str) != EOF) {
		link_list_node *p_head = create();
		//判断一下原来数据长度
		int len = strlen(old_str);
		//开始循环整个数据体
		for(int i = 0;i < len;i++) {
			//来判断一下不等于[,或者这个]的情况
			if(old_str[i] != '[' && old_str[i] != ']') {
				//上面这种情况可以直接插入到链表中
				insert_elem(p_head,old_str[i],get_len(p_head));//这种情况直接尾插
			}else if(old_str[i] == '[') {
				//这种情况就要进行头插
				i++;
				int index = 0;//给一个插入的索引
				while(i < len) {
					if(old_str[i] == ']' || old_str[i] == '\0') {
						break;//跳出
					}
					if(old_str[i] != '[') {
						//这种情况始终头插
						insert_elem(p_head,old_str[i],index++);
					}
					i++;
				}					
			}	
		}
		//直接打印这个字符
		print_char(p_head);
		//这里做完了必须销毁
		destroy(p_head);
	}
	return 0;
}

运行结果: 

 

上面就是用我们动态库里面的单链表来解决了问题,下面我们用静态库也可以来解决问题

uva11988_2.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <staticlist.h>
#define ARR_MAX_SIZE 100005

int main()
{
	//还是要来输入数据
	char old_str[ARR_MAX_SIZE];
	while(scanf("%s",old_str) != EOF) {
		//创建静态表
		t_node  * p_head = create_list();
		//判断一下原来数据长度
		int len = strlen(old_str);
		//开始循环整个数据体
		for(int i = 0;i < len;i++) {
			//来判断一下不等于[,或者这个]的情况
			if(old_str[i] != '[' && old_str[i] != ']') {
				//上面这种情况可以直接插入到链表中
				insert_elem(p_head,old_str[i],get_len(p_head));//这种情况直接尾插
			}else if(old_str[i] == '[') {
				//这种情况就要进行头插
				i++;
				int index = 0;
				while(i < len) {
					if(old_str[i] == ']' || old_str[i] == '\0') {
						break;//跳出
					}
					if(old_str[i] != '[') {
						//这种情况始终头插
						insert_elem(p_head,old_str[i],index++);
					}
					i++;
				}					
			}	
		}
		//while结束之前打印数据
		int index = 0;
		while(index < get_len(p_head)) {
			node res = get_elem_by_pos(p_head,index);
			printf("%c",(char)res.data);
			index++;
		}
		destroy_list(p_head);
	}
	return 0;
}

运行结果:

下面还是利用静态表的设计思想来做只是我们不用库来做,而是利用一个整型数组,来保存原数组中相应值正确的位置,然后在整型数组中链起来。

大致思想如下(可参考可不参考)

上面我们利用i比cur多一位的特性,来交换数据

这个时候我们就必须考虑last应该在什么位置?

必须明白,last不是在next数组的最后一个位置里面,而是可能在中间或者其他位置,那么他的移动怎么来考虑,自然肯定是跟着i移动,它的位置最后始终停留在0这个数据位置,也就是末尾

但是可能last的移动有没有可能会存在问题,也就是说,last如果想cur一样跟着i移动,那么当遇到 ] 这个符号的时候,怎么去找0这个位置呢,所以,它跟着移动的条件必然是last == cur的时候,往下正常移动,否则会停在0这个位置

话不多说,直接上代码:uva11988_2.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100000

int main()
{
	char old_str[MAX_SIZE];
	while(scanf("%s",old_str + 1) == 1) {
		int cur;
		int last;
		cur = last = 0;
		int n = strlen(old_str + 1);
		int i,j;
		char ch;
		int next[MAX_SIZE];
		next[0] = 0;//这个0代表新数组的结尾
		for(i = 1;i <= n;i++) {
			ch = old_str[i];
			if(ch == '[') {
				cur = 0;
			}
			if(ch == ']') {
				cur = last;
			}
			if(ch != ']' && ch != '[') {
			 	//这里就要在新数组中交换索引
				//新数组第一个是next[0]->永远从这个位置开始启动i = next[i]
				next[i] = next[cur];//把i放到cur = 0前面
				next[cur] = i;
				if(last == cur) {
					last = i;
				}
				cur = i;
			}
		}
		//打印
		for(j = next[0]; j != 0;j = next[j]) {
			//这里面的i,就是实际数组的真实顺序
			printf("%c",old_str[j]);
		}
		printf("\n");	
	}
	return 0;	
}

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值