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

简介: 点击打开链接uva 11988 思路: deque模拟 分析: 1 题目给定一个字符串要求通过一序列的模拟输出最后的字符串 2 根据题目的意思[],分别表示的是键盘上的home和end键,home键的作用是跳到起始位置,end的作用是到最后一个位置。

点击打开链接uva 11988

思路: deque模拟
分析:
1 题目给定一个字符串要求通过一序列的模拟输出最后的字符串
2 根据题目的意思[],分别表示的是键盘上的home和end键,home键的作用是跳到起始位置,end的作用是到最后一个位置。
3 根据2我们可以利用双端队列来模拟,如果是[我们插入front,如果是]插入back,最后在输出即可

代码:

#include<deque>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 100010;

char str[MAXN];
deque<string>dqe;

void insert(bool front , bool rear , string s){
    if(front)
        dqe.push_front(s);
    if(rear)
        dqe.push_back(s);
}

void output(){
    while(!dqe.empty()){
         cout<<dqe.front();
         dqe.pop_front();
    }
    puts("");
}

void solve(){
    int len = strlen(str); 
    bool front , rear;
    string s = "";
    front = true;
    rear = false;
    for(int i = 0 ; i < len ; i++){
        if(str[i] == '['){
           insert(front , rear , s);
           s = ""; 
           front = true;
           rear = false;
        }
        else if(str[i] == ']'){
           insert(front , rear , s);
           s = ""; 
           front = false;
           rear = true;
        }
        else{
           s += str[i]; 
        } 
    }
    insert(front , rear , s);
    output();
}

int main(){
    while(scanf("%s" , str) != EOF)
        solve();
    return 0;
}


list 来做

#include<list>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 100010;
list<char>ls;

int main(){
    char str[MAXN];
    while(gets(str)){
         ls.clear();
         int len = strlen(str); 
         list<char>::iterator it = ls.begin();
         for(int i = 0 ; i < len ; i++){
             if(str[i] == '[') 
                it = ls.begin();
             else if(str[i] == ']') 
                it = ls.end();
             else{
                ls.insert(it,str[i]);
             } 
         }
         for(it = ls.begin(); it != ls.end() ; it++)
             printf("%c" , *it);
         puts("");
    } 
    return 0;
}




目录
打赏
0
0
0
0
15
分享
相关文章
C#: Get current keyboard layout\input language
原文 https://yal.cc/csharp-get-current-keyboard-layout/   On some occasions, you may want to get a "global" input language - that is, the keyboard layo...
1113 0
Latex "Error: Extra alignment tab has been changed to \cr. "
Latex 编译时出现 Error: Extra alignment tab has been changed to \cr.  是因为\begin{tabular}后面的参数指定为7列,而实际排了8列数据。
4265 0
【1084】Broken Keyboard (20 分)
在第二个字符串中出现,则跳出内层for循环;内层for循环结束时,如果第二个字符串未出现c1,且c1未被输出过,则输出c1。 对于上面的判断c1是否输出过:hashtable数
103 0
DT Slate Brush Browser Plug -in description
DT Slate Brush Browser Plug -in description
84 0
Using Mimikatz Alpha or Getting Clear Text Passwords with a Microsoft Tool
Mimikatz is now built into Metasploit's meterpreter, you can do load mimikatz from the meterpr...
984 0
CExtStatusControlBar - Managing status bar's panes is getting easier
Is your email address OK? You are signed up for our newsletters but your email address is either unconfirmed, or has not been reconfirmed in a long time.
1149 0
uva674Coin Change
题意:手中的硬币币值有1,5,10,25,50共5种,给定一个面值n,问把n兑换成硬币的方案总数是多少。 分析:先打表,再输入输出。动态规划的简单题目,设dp[i]表示面值为i的情况下能兑换的种类,那么dp[i]=sigma(dp[i-v[j]]), j=0..4, v[j]={1,5,10,25,50};也就是,如果i大于v[j],说明能够用dp[i-v[j]]的方案再加上一枚面值为v[j]的硬币作为面值i的方案,不过这只是方案中硬币的数量多了一枚,题目中只是问方案数量,那么此时两者在方案数量上等价,那么方案总数上加上这一种情况就可以了。
735 0