UVA 11988 Broken Keyboard 链表

UVAOJ时不时掉链子

点击打开题目链接

用链表模拟一下就ok了

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

int main()
{
    list<char> srcList, ansList;
    list<char>::iterator listIt;

    string str;
    while (cin >> str)
    {
        srcList.clear();
        ansList.clear();
        for (int i = 0; i < str.length(); i++)  //将输入的字符串添加到链表
            srcList.push_back(str[i]);
        srcList.push_back(']');                 //防止漏掉最后的字符串
        char preChar = '[';                 //记录前一个按键
        string curStr = "";             //记录按键后面的字符串
        for (listIt = srcList.begin(); listIt != srcList.end(); listIt++)   //遍历源字符串
        {
            if (*listIt == '[' || *listIt == ']')             //碰到按键
            {
                if (preChar == '[')         //Home键,插入到头部,注意插入顺序
                {
                    for (int i = curStr.length() - 1; i >=0 ; i--)
                        ansList.push_front(curStr[i]);
                }
                else                //End键
                {
                    for (int i = 0; i < curStr.length(); i++)
                        ansList.push_back(curStr[i]);
                }
                preChar = *listIt;      //更新上次的按键
                curStr = "";            //清空字符串
            }
            else
            {
                curStr += *listIt;      //不是按键的话记录字符串
            }
        }

        for (listIt = ansList.begin(); listIt != ansList.end(); listIt++)
            cout << *listIt;
        cout << endl;
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值