CodeForces 363C - Fixing Typos

Many modern text editors automatically check the spelling of the user'stext. Some editors even suggest how to correct typos.

In this problem your task to implement a small functionality to correcttwo types of typos in a word. We will assume that three identical letterstogether is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical lettersimmediately followed by another couple of identical letters is a typo too (forexample, words "helloo" and "wwaatt" contain typos).

Write a code that deletes the minimum number of letters from a word,correcting described typos in the word. You are allowed to delete letters fromboth ends and from the middle of the word.

Input

The single line of the input contains word s, its length is from 1 to 200000 characters. The given word s consists of lowercase English letters.

Output

Print such word t that it doesn't contain any typos described in the problem statement andis obtained from s by deleting the least number of letters.

If there are multiple solutions, print any of them.

Sample test(s)

input

helloo

output

hello

input

woooooow

output

woow

Note

The second valid answer to the test from the statement is "heloo".

 

思路:

有两种输入错误:

1、 同一个字符连续出现3次

2、 两对字符相连


代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int main()
{
	int len;
	char temp[200005];
	while (scanf("%s", temp) != EOF)
	{
		char c;
		string st;
		int l = strlen(temp);
		int i;
		bool flag = false;
		st = temp[0];
		for (i = 1; i<l; i++)
		{
			c = temp[i];

			len = st.length();

			if (c == st[len - 1])
			{
				if (!flag)
				{
					st += c;
					flag = true;
				}
			}
			else
				st += c;

			len = st.length();
			if (len>3 && st[len - 1] != st[len - 2] && st[len - 2] != st[len - 3])
				flag = false;
		}
		cout << st << endl;
	}

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值