【淘宝】测试编程题(最新)
题目:
输入一串字母,等于或超过三个则消除。
如:
输入: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;
}
}