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

本文介绍了一种特殊情况下(键盘的“Home”和“End”键偶尔自行触发)的文本处理算法,通过使用栈和队列等数据结构,实现对输入文本的重新组织,以还原用户实际意图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目来源:https://cn.vjudge.net/problem/UVA-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).You’re not aware of this issue, since you’re focusing on the text and did not even turn on themonitor! 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.

Input

There are several test cases. Each test case is a single line containing at least one and at most 100,000letters, underscores and two special characters ‘[’ and ‘]’. ‘[’ means the “Home” key is pressedinternally, 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

题意:'['为home键,换到开头,']'为end换到末尾

分析:只需将按了home键的单词用一个栈进行存储,然后依次输出即可

于是果断的来了一个string 和stack,结果TLE了,


TLE的代码

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <time.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <sstream> 
#define ll long long
#define eps 1e-6
#define pi (acos(-1))
#define e exp(1.0)
#define cei(n) ((int)ceil((n)))
#define rou(n) ((int)round((n)))
#define qclear(q) while(!q.empty())q.pop();
using namespace std;
template<class T> T gcd(T a, T b) { return b ? gcd(b, a%b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a,b) * b;    }
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	#ifndef ONLINE_JUDGE
		freopen("in.txt","r",stdin);
		freopen("out.txt","w",stdout);
	#endif
	stack<string>st;
	string s,s1,s2;
	while(cin>>s)
	{
		int k=0;
		s1.clear();
		s2.clear();
		qclear(st);
		for(int i=0;i<s.length();i++)
		{
			if(s[i]=='[')
			{
				if(k)
				{
					if(s2.length()>=1)
					st.push(s2);
					s2.clear();
				}
				k=1;
				
			}
			else if(s[i]==']')
			{
				k=0;
				if(s2.length()>=1)
				st.push(s2);
				s2.clear();
			}
			else 
			{
				if(!k)s1=s1+s[i];
				else s2=s2+s[i];
			}
		}
		if(k)
		{
			if(s2.length()>=1)
			st.push(s2);
			s2.clear();
		}
		while(!st.empty())
		{
			cout<<st.top();
			st.pop();
		}
		cout<<s1<<endl;
	}
} 



然后看着自己用的cin,和cout。想着肯定是输入的问题,就将输入换成scanf了。

于是想着用什么来替代里面的string,输入应该是先进先出的,就改成queue来替代string

用时170MS,想着输入竟然还是差这么多


AC代码:


#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <time.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <sstream> 
#define ll long long
#define eps 1e-6
#define pi (acos(-1))
#define e exp(1.0)
#define cei(n) ((int)ceil((n)))
#define rou(n) ((int)round((n)))
#define qclear(q) while(!q.empty())q.pop();
using namespace std;
template<class T> T gcd(T a, T b) { return b ? gcd(b, a%b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a,b) * b;    }
char s[100005],s1[100005],s2[100005];
int main()
{
	#ifndef ONLINE_JUDGE
		freopen("in.txt","r",stdin);
		freopen("out.txt","w",stdout);
	#endif
	stack<queue<char> >st;
	queue<char>q;
	while(~scanf("%s",s))
	{
		//初始化 
		int k=0,f=0;
		qclear(st);
		qclear(q);
		int n=strlen(s);
		//遍历 
		for(int i=0;i<n;i++)
		{
			if(s[i]=='[')//按了home变成开头 
			{
				if(f)//按了两次[[的情况 
				{
					if(q.size()>=1)
					st.push(q);	
					qclear(q);
				}
				f=1;
			}
			else if(s[i]==']')//按了end变成末尾 
			{
				f=0;
				if(q.size()>=1)//存入 
				st.push(q);
				qclear(q);
			}
			else 
			{
				if(!f)s1[k++]=s[i];//记录没有变顺序的字符 
				else q.push(s[i]);//记录换到开头的字符 
			}
		}
		if(f)//按了'['就没有 end的情况 
		{
			if(q.size()>=1)
			st.push(q);
		}
		s1[k]=0;
		//输出 
		while(!st.empty())
		{
			q=st.top();
			while(!q.empty())
			{
				putchar(q.front());
				q.pop();
			}
			st.pop();
		}
		puts(s1);
	}
} 
/*
asdofh[agasdf
asdfsa[sadfsa[asdfasd
*/



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值