Let's call left cyclic shift of some string t1t2t3…tn−1tn�1�2�3…��−1�� as string t2t3…tn−1tnt1�2�3…��−1���1.
Analogically, let's call right cyclic shift of string t� as string tnt1t2t3…tn−1���1�2�3…��−1.
Let's say string t� is good if its left cyclic shift is equal to its right cyclic shift.
You are given string s� which consists of digits 0–9.
What is the minimum number of characters you need to erase from s� to make it good?
Input
The first line contains single integer t� (1≤t≤10001≤�≤1000) — the number of test cases.
Next t� lines contains test cases — one per line. The first and only line of each test case contains string s� (2≤|s|≤2⋅1052≤|�|≤2⋅105). Each character si�� is digit 0–9.
It's guaranteed that the total length of strings doesn't exceed 2⋅1052⋅105.
Output
For each test case, print the minimum number of characters you need to erase from s� to make it good.
Example
input
Copy
3 95831 100120013 252525252525
output
Copy
3 5 0
Note
In the first test case, you can erase any 33 characters, for example, the 11-st, the 33-rd, and the 44-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 s� is already good.
题目大意:
给一个数字字符串,在删掉其中一些数字字符后,使得其左移x位和右移x位的结果相等。
问最少要删除多少数字?
思路:
能使数字字符串要使得其左移x位和右移x位的结果相等的字符串有两种:
1、只有一种数字字符。
2、只有两种数字字符且数量相等,还需要错位排列。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl "\n"
void solve(){
string s;
cin >> s;
ll sum=0,len=s.size();
for(char x = '0' ; x <= '9' ; x ++)
for(char y = '0' ; y <= '9' ; y ++){
ll cnt = 0;
char c=x;
for(ll i = 0 ; i < len ; i ++)
if(s[i] == c)
cnt++ ,c = c == x ? y : x;
sum = max(sum,cnt-(x != y && cnt&1) );
}
cout << len-sum << endl;
return;
}
int main(){
ll t=1;cin >> t;
while(t--)solve();
return 0;
}
文章讨论了如何通过删除数字字符,使得给定的数字字符串在左右移位后结果相等。提供了一个编程解题思路,涉及字符计数和条件判断。
596

被折叠的 条评论
为什么被折叠?



