UVA

来源:互联网 发布:php经典书籍推荐 编辑:程序博客网 时间:2024/06/01 22:43

题目来源: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_JUDGEfreopen("in.txt","r",stdin);freopen("out.txt","w",stdout);#endifstack<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_JUDGEfreopen("in.txt","r",stdin);freopen("out.txt","w",stdout);#endifstack<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[agasdfasdfsa[sadfsa[asdfasd*/



原创粉丝点击