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

2023-05-16

C. Good String

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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, stringand 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 ss, 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⋅1051≤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≤n0≤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

input

Copy


4
good
  

output

Copy


0
good
  

input

Copy


4
aabc
  

output

Copy


2
ab
  

input

Copy


3
aaa
  

output

Copy


3
  

 

 

题意:题目中规定合格的字符串的标准:1,长度为偶数 2,字符串奇数位和下一位即偶数位数字不能相等。求在原字符串中最少删除几个字符使其成为符合要求的字符串。

 

我的代码:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <stack>

using namespace std;
stack<char>s;
char a[400005];
stack<char>ch;

void prtstk(){
	while(!s.empty()){
		ch.push(s.top());
		s.pop();
	}
	while(!ch.empty()){
		cout<<ch.top();
		s.push(ch.top());
		ch.pop();
	}
	cout<<endl;
}

int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		while(!s.empty())
		{
			s.pop(); 
		}
		while(!ch.empty())
		{
			ch.pop(); 
		}
		scanf("%s",a);
		int len = strlen(a);
		for(int i=0;i<len;i++)
		{
			if(i == 0)
			{
				s.push(a[i]);
			}
			else
			{
				if(s.size()%2==0)
				{
					s.push(a[i]);
				}
				else
				{
					if(s.top()!=a[i])
					{
						s.push(a[i]);
					}
				}
			}
		}
		if(s.size()%2==1)
		{
			printf("%d\n",n-s.size()+1);
			s.pop();
			if(s.size()!=0)
			{
				prtstk();
			}
		}
		else
		{
			printf("%d\n",n-s.size());
			if(s.size()!=0)
			{
				prtstk(); 
			}
		}
	}
	return 0;
}

 

容易错的样例:

输入

4

gdoo

5

aaaaa

5

aabba

输出

gd

            (为空串)

abba

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐

  • Go调用Python by go-python3

    确保python版本为3 7 conda create go python span class token assign left variable python span span class token operator 61 spa
  • linux下搭建maven私服

    maven私服我相信很多公司都有 xff0c 私服的好处有以下几点 xff1a 1 节省下载资源开销 jar包 xff08 不一定是jar xff0c 也可以是其他资源 xff09 都存在私服 xff0c 可以不必每次下载都去远程仓库去下载
  • git 安装包 最新 下载 快速 国内 镜像 地址

    下载git时 xff0c 先进官网看 https git scm com download win 然后发现几kb的网速 xff0c 这是要让我下一年么 xff0c 找了找网上有没有其他的镜像 xff0c 发现阿里有一个镜像 xff0c 下
  • docker笔记(四、docker部署beego打包后的二进制文件)

    在beego工程里 xff0c 使用go build可以将该工程打包成一个二进制文件 xff0c 那么这个二进制文件在docker里面该怎么部署呢 xff1f 先写一个简单的图片上传的demo xff0c 名字叫docker test 在工
  • LINUX服务器最简洁的HTTPS免费证书配置方法

    注意 xff1a 该方法已在多台服务器配置了免费的https证书 xff0c 无论是更新还是第一次配置都运行成功 xff1b 由于是免费版 xff0c 每个证书都只有三个月的有效期 xff0c 也无法保证安全和稳定性 xff0c 所以只建议
  • 【性能测试】获取性能系统指标之示例Python代码

    usr bin env python coding utf 8 import sys import datetime import time import psutil from ctypes import 34 34 34 性能测试示例P
  • select I/O 多路复用实现服务器聊天室功能

    基本概念 IO多路复用是指内核一旦发现进程指定的一个或者多个IO条件准备读取 xff0c 它就通知该进程 IO多路复用适用如下场合 xff1a xff08 1 xff09 当客户处理多个描述字时 xff08 一般是交互式输入和网络套接口 x
  • iOS-使用Masnory实现UITableViewCell自适应高度

    在iOS开发当中 xff0c 如果涉及到UITableViewCell的一些复杂UI的绘制时难免会碰到这么一个难题 xff1a UITableViewCell的高度如何设置 xff01 的确 xff0c 我们就拿一个简单的例子来说 xff1
  • ubuntu中共享文件夹看不到

    博主的ubuntu安装VMwaretools后共享文件夹设置完发现在 mnt hgfs总看不到 经过多次摸索后终于可以了 首先要使用root用户登陆ubuntu 然后再安装VMwaretools 在设置共享文件夹 然后解决挂在的问题 1 设
  • keystore was tampered with,or password was incorrect解决办法

    利用keytool导入证书 xff0c 命令如下 keytool import alias HZZSQKJdianshang file HZZSQKJdianshang cer keystore trust jks storepass st
  • Convert Picture or Video to ascii

    一个利用ascii拼成的谷歌街景地图 xff01 http tllabs io asciistreetview 看上去效果真不错 xff01 除此之外 xff0c linux下面也有类似的ascii艺术 xff0c 比如 aview asc
  • texlive2015-6安装

    http www cnblogs com snake553 p 4831588 html CentOS6 5 下安装 texlive2015 并设置 ctex 中文套装 0 卸载旧版本的 texlive 0 1 卸载 texlive2007
  • ubuntu20.04 安装显卡驱动后开机卡住,无法进入登陆界面问题解决

    ubuntu20 04 安装显卡驱动后开机卡住 xff0c 无法进入登陆界面问题解决 进入recovery mode 禁用nvidia drm systemctl isolate multi user target modprobe r n
  • Linux环境变量失效,命令不可用

    背景 linux在修改完环境变量 etc profile后保存文件后 xff0c 发现大多数命令不可用 xff0c 只有少数如 xff1a cd pwd可以使用 xff1b 原因分析 1 etc profile文件中有无效字符或变量 xff
  • Ubuntu20.04安装MySQL5.7-实测3种方法(保姆级教程)

    最近生产系统系统需要使用MySQL5 7版本的数据库 xff0c 而Ubuntu20 04默认是8 0的版本 xff0c 折腾了一段时间后 xff0c 测试了3中方法 xff0c 在实际应用环境中测试成功 xff0c 因此发布出来给大家参考
  • 数据仓库的源数据类型

    数据仓库中集成了企业几乎所有的可以获取到的数据以用于数据分析和决策支持 xff0c 当然也包括了我在网站分析的数据来源一文中所提到的所有数据 这些进入到数据仓库中的数据无外乎三种类型 xff1a 结构化数据 半结构化数据 和非结构化数据 x
  • soj 1075 拓扑排序队列实现

    就是soj 拓扑排序的模板题吧 然后我中午把用队列实现的拓扑排序的方法看了下 晚上就打算来练一下这种纯模板 对于这实现的方法 xff0c 我的理解就是存下每个节点的入度以及它指向的其他节点 xff0c 由于指向多少个这个不太能确定所以用一个
  • poker

    include lt stdbool h gt include lt stdio h gt include lt stdlib h gt define NUM RANKS 13 define NUM SUTTS 4 define NUM C
  • vue实现筛选动态配置

    lt el form class 61 34 cellStyleForm 34 model 61 34 queryParams 34 ref 61 34 queryForm 34 v show 61 34 showSearch 34 inl
  • C. Good String Codeforces Round #560 (Div. 3)

    C Good String time limit per test 1 second memory limit per test 256 megabytes input standard input output standard outp