Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- string A, B;
- int k, n, fr;
- char ch;
- char findMaxFrequency(string s, int &frecv) /// caut ch cu cea mai mare frecventa
- {
- unordered_map<char, int> Hash;
- sort(B.begin(), B.end());
- for(int i=0; s[i]; i++)
- Hash[s[i]]++;
- int max_count = 0;
- char res;
- for(auto i : Hash) {
- if(max_count < i.second) {
- res = i.first;
- max_count = i.second;
- }
- }
- frecv=max_count; /// frecventa maxima si
- return res; /// caracterul
- }
- unsigned long long power(int x, unsigned int y)
- {
- unsigned long long res = 1;
- while (y > 0)
- {
- if (y & 1)
- res =1ULL* res * x;
- y = y >> 1;
- x = x * x;
- }
- return res;
- }
- int findLen(string& A, int n, int k, char chr) /// aici caut sa modific
- { /// chr este caracaterul cu fr cea mai mare
- int l=0, r=0, p=1,cnt=0;
- for(int i=0; A[i] && k; i++)///parcurg sirul cat timp am euro(k) si am caractere in sir
- if(A[i]!=chr) /// daca difera stabilesc interval si numar cate difera
- ++r, cnt++;
- else
- {
- p=power(2,r-l+1); /// acesta este costul unui interval
- k-=p; /// scad costul intervalului ....
- int j=i;
- while(A[j]==chr && A[j]) /// daca nu difera parcurg numai
- j++;
- if(A[j]) /// daca mai am caractere
- {
- i=j-1;
- l=j;
- r=j;
- }
- }
- return cnt;
- }
- int main()
- {
- getline(cin,A);
- cin>>k;
- B.assign(A);
- n=A.size();
- ch=findMaxFrequency(B,fr);
- cout<<findLen(A, n, k, ch) + fr;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement