Codeforces Round #560 (Div. 3) C. Good String

2023-05-16

Let’s call (yet again) a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, string and xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good.

You are given a string s, you have to delete minimum number of characters from this string so that it becomes good.

Input
The first line contains one integer n (1≤n≤2⋅105) — the number of characters in s.

The second line contains the string s, consisting of exactly n lowercase Latin letters.

Output
In the first line, print one integer k (0≤k≤n) — the minimum number of characters you have to delete from s to make it good.

In the second line, print the resulting string s. If it is empty, you may leave the second line blank, or not print it at all.

Examples
inputCopy
4
good
outputCopy
0
good
inputCopy
4
aabc
outputCopy
2
ab
inputCopy
3
aaa
outputCopy
3

非常好想,只要按顺序比较字符是否相同,记录下标来判重即可。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<string>
using namespace std;
int main() {
	ios::sync_with_stdio(false);
	int n;
	char a[200005], b[200005];
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	if (n == 1) {
		cout << '0' << endl;
		return 0;
	}
	int k = 1;
	int x = 1;
	while (true) {
		if (x >= n)break;
		for (int j = x + 1; j <= n; j++) {
			if (a[j] != a[x]) {
				b[k] = a[x];
				b[k + 1] = a[j];
				k += 2;
				x = j + 1;
				break;
			}
			if (j == n)x = n;
		}
	}
	cout << n - k + 1 << endl;
	for (int i = 1; i < k; i++)cout << b[i];
	return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Codeforces Round #560 (Div. 3) C. Good String 的相关文章

随机推荐

  • SUNDIALS库的编译和使用

    SUNDIALS库的编译和使用 1 简介 SUNDIALS SUite of Nonlinear and DIfferential ALgebraic equation Solvers 是由美国劳伦斯利福摩尔国立实验室 xff08 Lawr
  • 【ing】在Linux虚拟机上安装Sundials库(图文)

    1 Sundials库下载 Sundials下载地址 2 具体步骤 2 1 下载sundials 2 2 0 本次尝试选择sundials 2 2 0进行安装 Sundials文件内容如下 xff1a 2 2 创建安装目录 安装目录名称为
  • 基于docker部署Prometheus

    文章目录 基于Docker搭建Prometheusgitee 介绍Prometheus一 安装运行Prometheus docker版 部署Prometheus1 安装docker联网状态下阿里云离线安装包下载2 下载镜像包3 启动node
  • 程序设计思维与实践 Week11 作业 E 选做题11-1 东东与 ATM

    题目描述 xff1a 一家银行计划安装一台用于提取现金的机器 机器能够按要求的现金量发送适当的账单 机器使用正好N种不同的面额钞票 xff0c 例如D k xff0c k 61 1 2 N xff0c 并且对于每种面额D k xff0c 机
  • kubectl edit

    文章目录 kubectl edit官方文档语法示例 kubectl edit 官方文档 使用默认编辑器 编辑服务器上定义的资源 使用命令行工具获取的任何资源都可以使用edit命令编辑 edit命令会打开使用KUBE EDITOR xff0c
  • kubectl exec

    文章目录 kubectl exec通过bash获得pod中某个容器的TTY xff0c 相当于登录容器 命令行 创建一个test文件 xff1a kubectl exec exec命令同样类似于docker的exec命令 xff0c 为在一
  • kubectl describe

    文章目录 describe语法选项 示例描述一个node详细信息描述一个pod描述calico yaml中的资源类型和名称指定的pod描述所有的pod描述所有包含label k8s app 61 calico kube controller
  • k8s自动化安装脚本(kubeadm-1.21.1)

    文章目录 介绍软件架构安装教程更新内容2023 02 102022 10 202022 08 06准备部署包 操作步骤环境准备结构备注 解压部署包修改host文件初始化环境验证ansible配置 安装k8s集群登录master的节点添加no
  • Shell——docker启动yapi

    文章目录 脚本简介脚本注解执行方式脚本内容 脚本简介 基于运维统一脚本中 17 平台管理下的Yapi管理平台部署系统版本Centos7docker环境 脚本注解 该脚本快速部署yapi平台 xff0c 已通过docker commit把对应
  • Shell——查看基础信息脚本

    文章目录 脚本简介脚本注解安装方式执行方式执行结果 脚本内容新版本旧版本 脚本简介 基于运维统一脚本中 xff0c 19 脚本安装下的检查服务器脚本安装使用yum安装 yum仓库 xff0c 系统版本Centos7 脚本注解 该脚本为了快速
  • k8s自动化安装脚本(kubeadm-1.23.7)

    文章目录 介绍软件架构版本介绍更新内容2023 02 192023 02 152023 02 142023 02 102022 10 202022 08 06准备部署包 操作步骤环境准备结构备注 解压部署包修改host文件脚本使用方式初始化
  • 在VS code中打开网页预览

    在VS code中打开网页预览 在平时进行前端设计的时候 xff0c 你是否会因为无法实时观察到网页的变化而苦恼 xff0c 每一次都要重新打开html文件的过程过于繁琐 xff0c 现在就有一种新的方式能够让你在coding的时候实时观察
  • 最小生成树+思维 扩散(洛谷 P1661)

    扩散 题目描述 一个点每过一个单位时间就会向四个方向扩散一个距离 xff0c 如图 图略 两个点a b连通 xff0c 记作e a b 当且仅当a b的扩散区域有公共部分 连通块的定义是块内的任意两个点u v都必定存在路径e u a0 e
  • C语言解决百钱买百鸡问题

    百钱买百鸡问题 穷举法举例 求解 百钱买百鸡 问题 xff1a 公鸡每只5钱 xff0c 母鸡每只3钱 xff0c 小鸡3只1钱 求解思路 xff1a 设公鸡数为x xff0c 母鸡数为y xff0c 小鸡数为z xff0c 则可以得到下面
  • 程序设计思维与实践 Week12 作业 C 必做题 - 3

    题目描述 xff1a 东东每个学期都会去寝室接受扫楼的任务 xff0c 并清点每个寝室的人数 每个寝室里面有ai个人 1 lt 61 i lt 61 n 从第i到第j个宿舍一共有sum i j 61 a i 43 43 a j 个人 这让宿
  • VMware虚拟机解决空间不足,增加磁盘空间(磁盘扩容)

    在使用VMware进行linux学习过程中有时会出现磁盘空间不足的情况 xff0c 但是之前一直是只要磁盘空间不足就直接重装系统 xff0c 持续一段时间后感觉计算机科班出生的人这样做有点侮辱 xff0c 所以就静心学习了扩充磁盘的过程 x
  • Visual Studio Code + PyQt5环境搭建

    文章目录 x1f34e 前言 x1f34e 1 PyQt5工具包安装 x1f34e 2 Visual Studio Code配置 x1f34e 3 Visual Studio Code里使用PyQt5 x1f34e 4 总结 x1f34e
  • 字符串

    题目描述 一天蒜头君得到一个字符串 xff0c 他把字符串的第 i 个字符串变成 i 个 例如字符串为 span style color c7254e 34 abc 34 span xff0c 蒜头君把字符串变成了 span style c
  • 解决ubuntu 22.04上teamViewer/toDesk闪退等问题

    解决办法 xff1a 同时安装teamviewer和向日葵等远程控制软件 xff0c 同时开 xff0c g了一个用另一个重启 向日葵官网下载 xff1a https sunlogin oray com download linux tea
  • Codeforces Round #560 (Div. 3) C. Good String

    Let s call yet again a string good if its length is even and every character in odd position of this string is different