Data Structure Note WEEK 3

本文记录了数据结构实战的第三周内容,包括UVa 11988 Broken Keyboard的问题分析与代码实现,CodeForces 1321C Remove Adjacent的解题思路和代码,以及CodeForces 670E Correct Bracket Sequence Editor的分析与解决方案。重点讨论了如何处理特殊键盘输入、删除相邻字符和编辑正确括号序列的问题。
摘要由CSDN通过智能技术生成

WEEK3-2020/9/29

Problem A - Broken Keyboard-UVa 11988

Difficuty: 2 | tag: deque/list

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

Analysis

Relatively simple, we can use deque or list to meet the demand.
Using List may be easier, for if we use deque we need reverse the item when insert front, while list needn’t。

MyCode

#include <iostream>
#include <algorithm>
#include <string.h>
#include <list>
using namespace std;

int main()
{
   
    ios::sync_with_stdio(false);
    string s;
    while(cin >> s)
    {
   
        list<char> line;
        auto it = line.begin();
        for(auto st : s)
        {
   
            if(st == '[')
                it = line.begin();
            else if(st == ']')
                it = line.end();
            else
            {
   
                it = line.insert(it, st);
                ++it;
            }            
        }

        for(auto word : line)
        {
   
            cout << word;
        }
        cout << endl;
    }
}

Problem B - Remove Adjacent CodeForces - 1321C

Difficuty: 2 | tag: greedy

刚刚折腾完增加字符的操作,现在就来搞删除吧,给你一个由小写字母组成的字符串s,现在要做删除操作,不过不能随便删,要求是某个字母的两边,存在一个字母序小1的字母的时候,它就可以删除,例如说,“cabcb”ÿ

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值