您的位置:首页 > 其它

UVA 11988 Broken Keyboard (a.k.a. Beiju Text) 字符串处理

2016-08-16 13:34 281 查看
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.

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

根据题意处理就好了

#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<stack>
#include<set>
#include<bitset>
using namespace std;
const int MAX=100005;
string Str;
bool used[MAX];
struct node
{
string S;
int num;
node(string temp,int n):S(temp),num(n) {};
};
bool cmp(node a,node b)
{
return a.num<b.num;
}
int main()
{
cin.sync_with_stdio(false);
while (cin>>Str)
{
memset(used,0,sizeof(used));
int len=Str.length();
int pos=0;
vector<node> vec;
for (int i=0; i<len; i++)
{
if (Str[i]==']')
{
used[i]=true;
continue;
}
if (Str[i]=='[')
{
int temp=i+1,j=1;
used[i]=true;
for (i=i+1,j=1; i<len; i++,j++)
{
used[i]=true;
if (Str[i]==']')
{
vec.push_back(node(Str.substr(temp,j-1),--pos));
break;
}
if (Str[i]=='[')
{
vec.push_back(node(Str.substr(temp,j-1),--pos));
i--;
break;
}
if (i==len-1)
{
vec.push_back(node(Str.substr(temp,j),--pos));
break;
}
}
}
}
sort(vec.begin(),vec.end(),cmp);
for (int i=0; i<(int)vec.size(); i++)
{
cout<<vec[i].S;
}
for (int i=0; i<len; i++)
{
if (!used[i]&&Str[i]!='['&&Str[i]!=']')
cout<<Str[i];
}
cout<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: