Codeforces 1389 C. Good String(枚举)

2024-04-16 00:32

本文主要是介绍Codeforces 1389 C. Good String(枚举),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Let’s call left cyclic shift of some string 𝑡1𝑡2𝑡3…𝑡𝑛−1𝑡𝑛 as string 𝑡2𝑡3…𝑡𝑛−1𝑡𝑛𝑡1.

Analogically, let’s call right cyclic shift of string 𝑡 as string 𝑡𝑛𝑡1𝑡2𝑡3…𝑡𝑛−1.

Let’s say string 𝑡 is good if its left cyclic shift is equal to its right cyclic shift.

You are given string 𝑠 which consists of digits 0–9.

What is the minimum number of characters you need to erase from 𝑠 to make it good?

Input
The first line contains single integer 𝑡 (1≤𝑡≤1000) — the number of test cases.

Next 𝑡 lines contains test cases — one per line. The first and only line of each test case contains string 𝑠 (2≤|𝑠|≤2⋅105). Each character 𝑠𝑖 is digit 0–9.

It’s guaranteed that the total length of strings doesn’t exceed 2⋅105.

Output
For each test case, print the minimum number of characters you need to erase from 𝑠 to make it good.

Example
inputCopy
3
95831
100120013
252525252525
outputCopy
3
5
0
Note
In the first test case, you can erase any 3 characters, for example, the 1-st, the 3-rd, and the 4-th. You’ll get string 51 and it is good.

In the second test case, we can erase all characters except 0: the remaining string is 0000 and it’s good.

In the third test case, the given string 𝑠 is already good.

题意:
求最少删除多少个字符,使得字符串左旋一次右旋一次相同(具体见题意)

思路:
最后肯定变成ABABAB形式
所以枚举AB就好了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>using namespace std;const int maxn = 2e5 + 7;char s[maxn];int main() {int T;scanf("%d",&T);while(T--) {scanf("%s",s + 1);int n = strlen(s + 1);int ans = n - 2;for(int i = 0;i <= 9;i++) {for(int j = 0;j <= 9;j++) {int now = i;int cnt = 0;for(int k = 1;k <= n;k++) {if(s[k] - '0' == now) {if(now == i) now = j;else now = i;} else {cnt++;}}if(now == i) {ans = min(ans,cnt);}}}printf("%d\n",ans);}return 0;
}

这篇关于Codeforces 1389 C. Good String(枚举)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/907342

相关文章

Java String字符串的常用使用方法

《JavaString字符串的常用使用方法》String是JDK提供的一个类,是引用类型,并不是基本的数据类型,String用于字符串操作,在之前学习c语言的时候,对于一些字符串,会初始化字符数组表... 目录一、什么是String二、如何定义一个String1. 用双引号定义2. 通过构造函数定义三、St

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

C#数据结构之字符串(string)详解

《C#数据结构之字符串(string)详解》:本文主要介绍C#数据结构之字符串(string),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录转义字符序列字符串的创建字符串的声明null字符串与空字符串重复单字符字符串的构造字符串的属性和常用方法属性常用方法总结摘

如何解决mysql出现Incorrect string value for column ‘表项‘ at row 1错误问题

《如何解决mysql出现Incorrectstringvalueforcolumn‘表项‘atrow1错误问题》:本文主要介绍如何解决mysql出现Incorrectstringv... 目录mysql出现Incorrect string value for column ‘表项‘ at row 1错误报错

java String.join()的使用小结

《javaString.join()的使用小结》String.join()是Java8引入的一个实用方法,用于将多个字符串按照指定分隔符连接成一个字符串,本文主要介绍了javaString.join... 目录1. 方法定义2. 基本用法2.1 拼接多个字符串2.2 拼接集合中的字符串3. 使用场景和示例3

C# string转unicode字符的实现

《C#string转unicode字符的实现》本文主要介绍了C#string转unicode字符的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录1. 获取字符串中每个字符的 Unicode 值示例代码:输出:2. 将 Unicode 值格式化

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

IDEA如何将String类型转json格式

《IDEA如何将String类型转json格式》在Java中,字符串字面量中的转义字符会被自动转换,但通过网络获取的字符串可能不会自动转换,为了解决IDEA无法识别JSON字符串的问题,可以在本地对字... 目录问题描述问题原因解决方案总结问题描述最近做项目需要使用Ai生成json,可生成String类型

C#实现获得某个枚举的所有名称

《C#实现获得某个枚举的所有名称》这篇文章主要为大家详细介绍了C#如何实现获得某个枚举的所有名称,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考一下... C#中获得某个枚举的所有名称using System;using System.Collections.Generic;usi