题目链接:
https://codeforces.com/problemset/problem/1389/C
思路:
自己写的时候也推出来了。
要么两个数字周期出现。并且长度必须为偶数
要么一个数字。
自己写的时候一直想直接构造出来…
没想到直接是暴力枚举。
对于字符串0-9这种范围还不太敏感。。
直接枚举第一个数字和第二个数字
取最少的就好。
注意:
第一个和第二个字母不同时,长度不能为奇数。如果是奇数,要-1。
AC代码
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
char s[N];
int t,n,ans;
int cnt;
int num;
int main()
{
cin>>t;
while(t--)
{
cin>>s;
ans=0;
ans=0x3f3f3f3f;
int n=strlen(s);
for(int i=0;i<=9;i++)
{
for(int j=0;j<=9;j++)
{
int x=0,y=0;
num=0;
for(int k=0;k<n;k++)
{
if(x==0&&s[k]-'0'==i)
{
num++;
x=1;
}
else if(x==1&&y==0&&s[k]-'0'==j)
{
num++;
y=1;
}
if(x==1&&y==1)
{
x=0;
y=0;
}
}
if(i!=j&&num%2!=0) num--;
ans=min(ans,n-num);
}
}
cout<<ans<<endl;
}
return 0;
}