测试用例有三个未通过,还需要改进!
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<set>
using namespace std;
int main()
{
int M;
cin >> M;//粘滞的次数
string str;
cin >> str;//待检测的字符串
set<string>s;
set<string>sm;
for (int i = 0; i < str.size(); i++)
{
int cnt = 0;//统计重复的数量
vector<int>v;
for (int j = i+1; j < str.size(); j++)
{
if (str[i]==str[j])
{
cnt++;
}
else if(cnt<M-1)
{
cnt = 0;
string temp(1, str[i]);
sm.insert(temp);
break;
}
else
{
v.push_back(cnt);
cnt = 0;
break;
}
}
//for (auto d : sm)cout << d << " ";
//cout << endl;
for (auto p : v)
{
if ((p+1)%M!=0)//如果存在不是M的倍数则跳出循环
{
break;
}
int n = (p + 1) / M;
string temp_1(1, str[i]);
string temp(n, str[i]);
if (find(sm.begin(), sm.end(), temp) == sm.end())//如果没有查询到正常的键盘就执行删除操作
{
s.insert(temp_1);
str = str.replace(i, p+1, temp);
i = i + p-1;
}
}
}
for (auto d : s)
std::cout << d;
std::cout << endl<<str << endl;
system("pause");
return 0;
}
网友评论