CodeForces - 363C Fixing Typos

许多现代文本编辑器会自动检查用户文本的拼写。 一些编辑甚至建议如何纠正错别字。

在这个问题中你的任务是实现一个小功能来纠正一个单词中的两种类型的拼写错误。 我们假设三个相同的字母拼写错误(例如,单词“helllo”包含拼写错误)。 此外,一些相同的字母后面紧跟着另外两个相同的字母也是一个错字(例如,“helloo”和“wwaatt”字样包含拼写错误)。

编写一个代码,删除单词中的最小字母数,纠正单词中描述的拼写错误。 您可以从单词的两端和中间删除字母。

Input

输入的单行包含单词s,其长度为1到200000个字符。 给定的单词s由小写英文字母组成。

Output

打印这样的单词t,它不包含问题陈述中描述的任何拼写错误,并通过删除最少数量的字母从s获得。

如果有多个解决方案,请打印其中任何一个。

思路:用一个数组来存放符合条件的,每次进入数组时判断是否满足条件,如果满足就加入,否则继续下一次判断。

#include<set>
#include<map>
#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 10;
char a[maxn], b[maxn];
int main()
{
    cin >> a;
    int len = strlen(a);
    b[0] = a[0], b[1] = a[1];
    int pos = 2;
    for(int i = 2; i < len; ++i)
    {
        if(a[i] == b[pos - 1] && b[pos - 1] == b[pos - 2])
            continue;
        else if(a[i] == b[pos - 1] && b[pos - 2] == b[pos - 3])
            continue;
        b[pos++] = a[i];
    }
    b[pos++] = '\0';
    cout << b << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41785863/article/details/88397769