我是靠谱客的博主 痴情棉花糖,这篇文章主要介绍题解 CF1389C 【Good String】,现在分享给大家,希望可以做个参考。

简要题意:

对于一个字符串 t 1 , t 2 , ⋯   , t n t_1,t_2,cdots,t_n t1,t2,,tn

我们定义它的右移为 t n , t 1 , ⋯   , t n − 1 t_n,t_1,cdots,t_{n-1} tn,t1,,tn1

我们定义它的左移为 t 2 , t 3 , ⋯   , t 1 t_2,t_3,cdots,t_{1} t2,t3,,t1

现在请问对于给出的一个字符串,最少删掉多少个字符才能是这个字符串的左移和右移完全相同?

我们来分析一下性质,比较一下右移和左移

右移: t n , t 1 , ⋯   , t n − 1 t_n,t_1,cdots,t_{n-1} tn,t1,,tn1

左移: t 2 , t 3 , ⋯   , t 1 t_2,t_3,cdots,t_{1} t2,t3,,t1

要使这 2 2 2 个东西相同就必须使得 t 2 = t n , t 1 = t 3 , ⋯   , t n − 1 = t 1 t_2=t_n,t_1=t_3,cdots,t_{n-1}=t_1 t2=tn,t1=t3,,tn1=t1

我们举个具体的例子,假设 n = 8 n=8 n=8

右移: t 8 , t 1 , t 2 , t 3 , t 4 , t 5 , t 6 , t 7 t_8,t_1,t_2,t_3,t_4,t_5,t_6,t_7 t8,t1,t2,t3,t4,t5,t6,t7

左移: t 2 , t 3 , t 4 , t 5 , t 6 , t 7 , t 8 , t 1 t_2,t_3,t_4,t_5,t_6,t_7,t_8,t_1 t2,t3,t4,t5,t6,t7,t8,t1

显然, t 2 = t 4 = t 6 = t 8 t_2=t_4=t_6=t_8 t2=t4=t6=t8 t 1 = t 3 = t 5 = t 7 t_1=t_3=t_5=t_7 t1=t3=t5=t7

可以证明当 n n n 为偶数时,这个字符串的循环节必须为 2 2 2

我再来试试 n = 7 n=7 n=7

右移: t 7 , t 1 , t 2 , t 3 , t 4 , t 5 , t 6 t_7,t_1,t_2,t_3,t_4,t_5,t_6 t7,t1,t2,t3,t4,t5,t6

左移: t 2 , t 3 , t 4 , t 5 , t 6 , t 7 , t 1 t_2,t_3,t_4,t_5,t_6,t_7,t_1 t2,t3,t4,t5,t6,t7,t1

显然, t 1 = t 3 = t 5 = t 7 = t 2 = t 4 = t 6 t_1=t_3=t_5=t_7=t_2=t_4=t_6 t1=t3=t5=t7=t2=t4=t6

可以证明当 n n n 为奇数时,这个字符串的循环节必须为 1 1 1

2 2 2 个东西笔者都试着证明了一下,有些麻烦,这里就不放了。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Problem: C. Good String // Contest: Codeforces - Educational Codeforces Round 92 (Rated for Div. 2) // URL: https://codeforc.es/contest/1389/problem/C // Memory Limit: 256 MB // Time Limit: 2000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) #include <bits/stdc++.h> using namespace std; typedef long long ll; template<typename T>inline void read(T &FF){ T RR=1;FF=0;char CH=getchar(); for(;!isdigit(CH);CH=getchar())if(CH=='-')RR=-1; for(;isdigit(CH);CH=getchar())FF=(FF<<1)+(FF<<3)+(CH^48); FF*=RR; } const int MAXN=2e5+10; int s[11][2]; int work(){ memset(s,0,sizeof(s)); string st;cin>>st; int n=st.size(),ans=n; st=' '+st; for(int i=0;i<=9;i++){ int s=0; for(int j=1;j<=n;j++) if(st[j]!=char(i+48))s++; ans=min(ans,s); } for(int i=0;i<=9;i++) for(int j=0;j<=9;j++){ int s=0; for(int k=1;k<=n;k++){ if(s%2==0){ if(st[k]==char(i+48))s++; }else{ if(st[k]==char(j+48))s++; } } if(s%2==1)s--; ans=min(ans,n-s); } cout<<ans<<endl; return 0; } int main(){ int T;read(T); while(T--){ //memset work(); } return 0; };

最后

以上就是痴情棉花糖最近收集整理的关于题解 CF1389C 【Good String】的全部内容,更多相关题解内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部