UVa 11988 - Broken Keyboard (a.k.a. Beiju Text) 题解

刘汝佳的题目,悲剧文本 -_-|||

这里使用vector<string>容器倒置记录数据,然后从后面输出就可以了。

难度就是不知道这样的文档到底哪里是开始输出,故此使用动态管理内存的容器比较好做。

增加了io处理的O(n)算法也没有上榜,郁闷。

#include <stdio.h>
#include <vector>
#include <string>
using std::vector;
using std::string;

const int MAX_B = 5120;
int id = 0, len = 0;
char buf[MAX_B];

inline char getFromBuf()
{
	if (id >= len)
	{
		len = fread(buf, 1, MAX_B, stdin);
		id = 0;
	}
	return buf[id++];
}

int oid = 0;
int FLASH_P = MAX_B - 2;
char outBuf[MAX_B];

inline void charToBuf(char c)
{
	if (oid > FLASH_P)
	{
		fwrite(outBuf, 1, oid, stdout);
		oid = 0;
	}
	outBuf[oid++] = c;
}

inline void flashLeft()
{
	if (oid) fwrite(outBuf, 1, oid, stdout);
}

int main()
{
	char c;
	while (c = getFromBuf())
	{
		if (!len) break;
		vector<string> vstr(1);
		int cur = 0;
		int end = 0;
		while (len && c != '\n')
		{
			if (c == '[')
			{
				vstr.push_back(string());
				end++;
				cur = end;
			}
			else if (c == ']')
			{
				if (vstr.size() > 1 && vstr[vstr.size()-1].empty())
					end--, vstr.pop_back();
				cur = 0;
			}
			else vstr[cur].push_back(c);
			c = getFromBuf();
		}

		for (int i = (int)vstr.size() - 1; i >= 0 ; i--)
		{
			for (int j = 0; j < (int)vstr[i].size(); j++)
			{
				charToBuf(vstr[i][j]);
			}
		}
		charToBuf('\n');
	}
	flashLeft();
	return 0;
}

UVa 11988 - Broken Keyboard (a.k.a. Beiju Text) 题解

时间: 2024-10-27 00:14:39

UVa 11988 - Broken Keyboard (a.k.a. Beiju Text) 题解的相关文章

uva 11988 - Broken Keyboard (a.k.a. Beiju Text)

题意:有一个键盘坏了  会在你不知道的情况下按下home或者end  给你这个键盘的实际输入  要求输出显示器上的实际显示 解析:输入最大5MB  直接数组检索肯定会超时,用 数组模拟链表 next数组表示显示屏中s[i]右边的字符编号,变量cur模拟光标,即当前光标位于s[cur]的右边. 变量last记录显示屏最后一个字符的下标. 1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 const in

UVA 11988 Broken Keyboard (a.k.a. Beiju Text) STL

题目链接: UVA...... 题目描述: 给出一个字符串, 其中遇到"["是光标到最前, 遇到']'时光标又回到最后, 输出最后的文本 解题思路: 用到STL, 设置变量flag维护光标的位置, 当遇到纯字母时, 组合起来当做字符串处理, 在遇到下一个非纯字母时, 根据flag的状态一起插进deque的最前或者最后 代码: #include <iostream> #include <cstdio> #include <string> #includ

链表 UVA 11988 Broken Keyboard (a.k.a. Beiju Text)

题目传送门 题意:训练之南P244 分析:链表模拟,维护链表的head和tail指针 #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; struct Link_list { char ch; Link_list *nex; }link_list[N]; int main(void) { while (true) { Link_list *head = link_list; Link_list *q = li

11988 - Broken Keyboard (a.k.a. Beiju Text)

Broken Keyboard (a.k.a. Beiju Text) 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 (inter

【日常学习】【非指针链表】Uva11988 - Broken Keyboard (a.k.a. Beiju Text)题解

这道题目拖了好几天,因为鄙人有两大天敌--链表和树TUT看了这个题材知道原来链表可以不用指针写,不过原理也是一样的,相当于是用数组模拟了个链表而不实用结构体,结构体里的指针就换成了两个变量cur和last了.这道题目本来测出来非常奇怪和合因为UVA AC HDU TLE SPOJ RE我正在奇怪,才发现同名的不同题目有三道TUT 题目的详解已经写在了注释里,上代码: #include<cstdio> #include<cstring> using namespace std; co

uva - Broken Keyboard (a.k.a. Beiju Text)(链表)

11988 - Broken Keyboard (a.k.a. Beiju Text) You’re typing a long text with a broken keyboard. Well it’s not so badly broken. The only problemwith the keyboard is that sometimes the “home” key or the “end” key gets automatically pressed(internally).Yo

Broken Keyboard (a.k.a. Beiju Text) UVA, 11988(链表)

Broken Keyboard (a.k.a. Beiju Text) From:UVA, 11988 Time Limit: 1000 MS Broken Keyboard (a.k.a. Beiju Text) 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 "hom

UVA11988 Broken Keyboard (a.k.a. Beiju Text)【数组模拟链表】

Broken Keyboard (a.k.a. Beiju Text) 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 (inter

N - Broken Keyboard (a.k.a. Beiju Text)

N - Broken Keyboard (a.k.a. Beiju Text) Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description 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 som