题目传送
题意:
给你一个字符串,如今有俩种操做,一种是字符串全部位都向左平移一个单位,另外一种是向右平移一个单位,如今要使得这个字符串俩种操做后的字符串仍是相等的,问最少改动多少个字符?ios
思路:
既然要平移后的字符串仍是彻底相等的,那么能够获得:
s[i-1] == s[i+1] (向右平移的和向左平移的),那么再推一下就能够获得只有俩种字符串是能够的:
1111111 或者 2525252525(这种只能是偶数)
那么因为是只有0到9的数字组成,那么固然也就20种状况,暴力跑一下就能够了c++
AC代码web
#include <bits/stdc++.h> inline long long read(){char c = getchar();long long x = 0,s = 1; while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();} while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();} return x*s;} using namespace std; #define NewNode (TreeNode *)malloc(sizeof(TreeNode)) #define Mem(a,b) memset(a,b,sizeof(a)) #define lowbit(x) (x)&(-x) const int N = 2e5 + 5; const long long INFINF = 0x7f7f7f7f7f7f7f; const int INF = 0x3f3f3f3f; const double EPS = 1e-7; const int mod = 1e9 + 7; const double II = acos(-1); const double PP = (II*1.0)/(180.00); typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; typedef pair<ll,ll> piil; signed main() { std::ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); int t; cin >> t; while(t--) { string s; cin >> s; int Min = INF,len = s.size(); for(int i = 0;i < 10;i++) { for(int j = 0;j < 10;j++) { int num = 0,x = 0,y = 0; for(int k = 0;k < len;k++) { if(!x && s[k]-'0' == i) num++,x = 1; else if(x && !y && s[k]-'0' == j) num++,y = 1; if(x && y) x = 0,y = 0; } if(i != j && num % 2 != 0) num--;//判断是哪一种状况 Min = min(Min,len-num);//更新改动的最少次数 } } cout << Min << endl; } }