Untitled
unknown
plain_text
7 months ago
2.0 kB
1
Indexable
Never
#pragma GCC optimize("O2,no-stack-protector,unroll-loops") #define ll long long #define pb push_back #define ipar(arr, n) vector<ll> arr(n); for(int i=0;i<n;i++) cin>>arr[i]; #include <cmath> #include <bits/stdc++.h> #define pii pair<int, int> #define pll pair<ll, ll> #define all(s) s.begin(),s.end() using namespace std; bool sorta(const pair<ll,ll>&a,const pair<ll,ll>&b) { return a.second<b.second;} bool sortd(const pair<ll,ll>&a,const pair<ll,ll>&b) { return a.second>b.second;} bool isPrime(ll n){if(n<=1)return false;if(n<=3)return true;if(n%2==0||n%3==0)return false;for(int i=5;i*i<=n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;} bool isPowerOfTwo(int n){if(n==0)return false;return (ceil(log2(n)) == floor(log2(n)));} bool isPerfectSquare(ll x){if (x >= 0) {ll sr = sqrt(x);return (sr * sr == x);}return false;} ll gcd(ll a, ll b){if (b == 0)return a;return gcd(b, a % b);} //__gcd ll lcm(ll a, ll b){return (a/gcd(a,b)*b);} void solve(){ string s;cin>>s; ll n=s.length(); vector<vector<ll>>occ(26,vector<ll>(n,0)); for (char ch = 'a'; ch <= 'z'; ch++) { occ[ch - 'a'][0] = (s[0] == ch) ? 1 : 0; for (ll i = 1; i < n; i++) { occ[ch - 'a'][i] = occ[ch - 'a'][i - 1] + ((s[i] == ch) ? 1 : 0); } } // // for (ll i = 0; i < 26; i++) { // cout << (char)('a' + i) << "-"; // for (ll j = 0; j < n; j++) // cout << occ[i][j] << " "; // cout << "\n"; // } unordered_map<char,ll>freq; for(ll i=0;i<n;i++){ freq[s[i]]++; } //END OF DS__init__ ll ans=INT_MAX; for(auto i:freq){ char ch=i.first; ll f=i.second; for(ll i=0;i+f<=n;i++){ ll p = occ[ch-'a'][i+f-1] - ((i > 0) ? occ[ch-'a'][i-1] : 0); ans=min(ans,f-p); } } cout<<ans<<"\n"; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); int t; cin>>t; while(t--) solve(); }
Leave a Comment