易证明,最后的字符串形式一定是 abab(ldots)ab 或 aaa(ldots)aaa 的形式
所以枚举 ab
注意 0a 的形式
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<stack>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn = 200010;
int n;
char s[maxn];
ll read(){ ll s=0,f=1; char ch=getchar(); while(ch<'0' || ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9'){ s=s*10+ch-'0'; ch=getchar(); } return s*f; }
int main(){
n = read();
for(int i=1;i<=n;i++){
scanf("%s",s+1);
int len = strlen(s+1);
int mi = 1000000007;
for(int k=10;k<=99;k++){
int t = 0,f = 0;
int a = k/10,b = k%10;
for(int j=1;j<=len;j++){
if(!f){
if(s[j]-'0' == a) f = !f;
}else{
if(s[j]-'0' == b){
f = !f;
++t;
}
}
}
// printf("t:%d
",t);
mi = min(mi,len-2 * t);
}
for(int k=0;k<=9;k++){
int t = 0,f = 0;
int a = 0,b = k;
for(int j=1;j<=len;j++){
if(!f){
if(s[j]-'0' == a) f = !f;
}else{
if(s[j]-'0' == b){
f = !f;
++t;
}
}
}
mi = min(mi,len-2 * t);
}
int cnt;
for(int k=0;k<=9;k++){
cnt = 0;
for(int j=1;j<=len;j++){
if(s[j]-'0' == k) ++cnt;
}
mi = min(mi,len-cnt);
}
printf("%d
",mi);
}
return 0;
}