UVA11988:悲剧文本(模拟链表)
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.
Sample Input
This_is_a_[Beiju]_text
[[]][][]Happy_Birthday_to_Tsinghua_University
Sample Output
BeijuThis_is_a__text
Happy_Birthday_to_Tsinghua_University
概译:我们在用键盘输入字符串,如果出现' [ '光标就跳到最前面,如果出现' ] '就跳到最后面,给出输入时的字符串输出实际得到的字符串,详见样例。
思路:水题轻喷……模拟,链表插入,可以用数组实现模拟链表。我模拟链表不太熟,第一次是用STL模拟的。可以用一个双端队列deque储存最终结果,遇到一个' [ '就把后面的字符放在stack里,再遇到' [ '或' ] '时把stack里的字符放在deque的front里。PS:可能WA的数据:abc[123[456[ef
STL比较慢,100msAC
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
while(getline(cin,s))
{
s+=']';
deque<char>dq;
stack<char>st;
bool state=false;
for (int i = 0; s[i]; ++i)
{
if (s[i]=='[') state=true;
else if (s[i]==']') state=false;
if(state)
{
if(s[i]!='[')
st.push(s[i]);
else
while(!st.empty())
{
dq.push_front(st.top());
st.pop();
}
}
else
{
if(s[i]!=']')
dq.push_back(s[i]);
while(!st.empty())
{
dq.push_front(st.top());
st.pop();
}
}
}
while(!dq.empty())
{
printf("%c",dq.front());
dq.pop_front();
}
printf("n");
}
return 0;
}
模拟链表是用一个next数组代替链表中的next指针,比如第一个字符s[1]的下一个是s[2],则next[1]=2。思想上和链表是一样的。另外众所周知,链表的题常常会把第0个作为不储存数据的辅助头结点,第一个下标才开始储存数据。
标程的思路是设置一个光标cur,但这个cur不是当前遍历到的位置i,而代表着位置i的字符应该插入在cur的右侧。期间cur有时会跳至左端即cur=0;有时要回到右端,所以还要开一个last变量保存最右端的下标,使cur=last跳回右端。
代码更精简,30ms,如下:
#include <bits/stdc++.h>
using namespace std;
#define maxl 100005
int main()
{
char s[maxl];
while(~scanf("%s",s+1))
{
int Next[maxl]={0};
int cur=0,last=0;
for (int i = 1; s[i]; ++i)
{
if(s[i]=='[') cur=0;
else if(s[i]==']') cur=last;
else
{
//链表插入操作
Next[i]=Next[cur];
Next[cur]=i;
//last的更新
if(cur==last) last=i;
//cur的更新
cur=i;
}
}
for (int i = Next[0]; i != 0; i = Next[i])
if (s[i]!='['&&s[i]!=']')
printf("%c",s[i]);
printf("n");
}
return 0;
}
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- VBA解压缩ZIP文件06——Huffman树码表
- 精品连载丨安卓 App 逆向课程之五 frida 注入 Okhttp 抓包下篇
- Excel VBA常用功能加载宏——工作表隐藏
- VBE菜单——CommandBars对象
- Java|屏幕截图
- VBA解压缩ZIP文件07——length和distance扩展
- Excel VBA常用功能加载宏——打开活动工作簿所在文件夹
- 常用功能加载宏——拆分工作表
- MyVBA加载宏——添加自定义菜单04——功能实现
- CS学习笔记 | 14、powerup提权的方法
- VBA解压缩ZIP文件05——Huffman树
- JavaScript|jQuery基础语法
- VBA解压缩ZIP文件03——解压准备工作
- VBA解压缩ZIP文件04——解析ZIP文件结构
- 开发|Springboot简单实现文件上传