uva11988 Broken Keyboard (a.k.a. Beiju Text)

来源:互联网 发布:ubuntu subline 编辑:程序博客网 时间:2024/06/16 18:16

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). The size of input file does not exceed 5MB.
Output

For each case, print the Beiju text on the screen.
Sample Input
This_is_a_[Beiju]_text
[[]][][]Happy_Birthday_to_Tsinghua_University
Output for the Sample Input

BeijuThis_is_a__text
Happy_Birthday_to_Tsinghua_University
这道题紫书上写的方法琢磨了好久,才想到了一些思路,总体是把这些输入字符串以[ 和]分成好几段,每一段都用链表即一个next数组链接起来,然后每一段的段尾再和下一段的段首连接起来,相当于按照顺序重组了一下.
具体操作的话其实就是模拟电脑输入,设置一个变量cur表示光标的位置,我们每输入一个字符时这个字符下一个字符就是原来光标的下一个字符,原来光标所指位置的字母的下一个字符变成了输入的这个字符.之后光标所指的位置更新成输入的这个字符.
代码:

////                       _oo0oo_//                      o8888888o//                      88" . "88//                      (| -_- |)//                      0\  =  /0//                    ___/`---'\___//                  .' \\|     |// './/                 / \\|||  :  |||// \//                / _||||| -:- |||||- \//               |   | \\\  -  /// |   |//               | \_|  ''\---/''  |_/ |//               \  .-\__  '-'  ___/-. ///             ___'. .'  /--.--\  `. .'___//          ."" '<  `.___\_<|>_/___.' >' "".//         | | :  `- \`.;`\ _ /`;.`/ - ` : | |//         \  \ `_.   \_ __\ /__ _/   .-` /  ///     =====`-.____`.___ \_____/___.-`___.-'=====//                       `=---='//////     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~////               God help me   No WA & TLE//////#include <bits/stdc++.h>typedef long long ll;#define inf 0x3f3f3f3fusing namespace std;const int N=1e5+10;int arr[N];int net[N];//next[i]表示当前显示屏中s[i]右边的字符编号int main(){    ios::sync_with_stdio(false);    string str;    while(cin>>str)    {        for(int i=str.size();i>=0;i--)            str[i]=str[i-1];        memset(net,0,sizeof net);        int ed=0;//最后一个字符的编号        int cur=0;//光标所在位置(光标在所指位置字符的右边,可以和字符在同一个位置)        for(int i=1;i<=str.size();i++)        {            if(str[i]=='[')               cur=0;//光标指向最左侧            else if(str[i]==']')                cur=ed;//光标指向"最后一个字符的编号"            else            {                net[i]=net[cur];//字母i的下一个字母是上一个指针所指字母的下一个字母                net[cur]=i;//上一个指针所指字符的下一个字符是字母i                //我们需要把新输入的字符插入到链表中,它的前后都需要处理                        if(cur==ed)//如果上一个位置指的是最后一个字符,那么更改最后一个字符为当前字符                    ed=i;                cur=i;//更改指针所指字符为当前字符            }        }        for(int i=net[0];i!=0;i=net[i])//i=0时即终止            cout<<str[i];        cout<<endl;    }    return 0;}
原创粉丝点击