【淘宝】测试编程题(最新)

【淘宝】测试编程题(最新)

题目:
输入一串字母,等于或超过三个则消除。
如:
输入:AAABCCDDDCB
输出:BB

输入:AABBBABBBA
输出:A


完整程序如下:

#include<iostream>
#include<stack>
#include<string>
using namespace std;

int main()
{
    string str;
    stack<pair<char,int> > s1;
    pair<char,int>  onechar;
    while(cin>>str)
    {
        for(int i=0; i<str.size(); i++)
        {
            if(s1.empty())
            {
                onechar.first=str[i];
                onechar.second=1;
                s1.push(onechar);
            }
            else if(s1.top().first==str[i])
            {
                onechar.first=str[i];
                //onechar.second=s1.top().second+1;
                onechar.second=++s1.top().second;
                s1.push(onechar);
            }
            else if(s1.top().first!=str[i])
            {
                onechar.first=str[i];
                if((s1.top().second)>=3)
                {
                    int num=s1.top().second;
                    while(num--)
                        s1.pop();

                    if(!s1.empty() && s1.top().first==str[i])
                    {
                        //onechar.second=s1.top().second+1;
                        onechar.second=++s1.top().second;
                    }
                    else
                        onechar.second=1;
                    s1.push(onechar);
                }
                else
                {
                    onechar.second=1;
                    s1.push(onechar);
                }
            }
        }
        //输出栈
        string result;
        result.resize(s1.size());
        int size=s1.size()-1;
        while(!s1.empty())
        {
            //cout<<s1.top().first;
            result[size--]=s1.top().first;
            s1.pop();
        }
        cout<<result<<endl;
        //cout<<endl;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值