Header Ad

HackerEarth Compare Strings problem solution

In this HackerEarth Compare Strings problem solution, You have been given two strings, A and B (of length N each) and Q queries.
The strings contain only 0s and/or 1s.

For every query, you are given an index i. You have to update the value at index i to 1 in string B and check if B is lexicographically equal to or larger than A or not.
If yes, then print "YES" and if not, print "NO".


HackerEarth Compare Strings problem solution


HackerEarth Compare Strings problem solution.

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pll pair<ll,ll>
#define pii pair<float,int>
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define a(x) scanf("%d",&x)
#define sll(x) scanf("%lld",&x)
#define pf(x) printf("%d\n",x)
#define pfl(x) printf("%lld\n",x)
#define forl(i,x,y) for(ll i=x;x<y;i++)
#define flash ios_base::sync_with_stdio(false);cin.tie(0)
#define MAX5 100005
#define MAX6 1000005
#define MLOG 18
#define limit 10e18
#define MAX 3000000000
ll power(ll x, ll y, ll p){
ll res = 1;
x = x % p;
while (y > 0){
if (y & 1)
res = (res*x) % p;
y = y>>1;
x = (x*x) % p;
}
return res;
}
ll fact[2*MAX5];
ll func(){
fact[0] = fact[1] = 1;
for(ll i = 2; i < 200001; i++){
fact[i] = (fact[i-1]*i)%mod;
}
}

ll arr[MAX6];
ll when[MAX6];
string a,b;
ll func(ll x){
string c = b;
for(ll i = 0; i <= x; i++){
c[arr[i]] = '1';
}
if(c >= a) return 1;
return 0;
}
int main(){
ll n,q;
freopen("in07.txt","r",stdin);
freopen("out07.txt","w",stdout);
sll(n);sll(q);
cin >> a;
cin >> b;
ll sum = 0;
for(ll i = 0; i < q; i++){
sll(arr[i]);
arr[i]--;
}
ll low = 0;
ll high = q-1;
ll ans = INT_MAX;
while(low <= high){
ll mid = (low + high)/2;
// cout << low << " " << high << " " << mid << " " << func(mid) << endl;
if(func(mid)){
high = mid-1;
ans = min(ans,mid);
}
else{
low = mid + 1;
}
}
//cout << ans << endl;
for(ll i = 0; i < n; i++){
if(i < ans) printf("NO\n");
else printf("YES\n");
}
}

Second solution

#include<bits/stdc++.h>
#define LL long long int
#define M 1000000007
#define MM 1000000009
#define reset(a) memset(a,0,sizeof(a))
#define rep(i,j,k) for(i=j;i<=k;++i)
#define per(i,j,k) for(i=j;i>=k;--i)
#define print(a,start,end) for(i=start;i<=end;++i) cout<<a[i];
#define endl "\n"
#define inf 100000000000000
LL pow(LL a,LL b,LL m){LL x=1,y=a;while(b > 0){if(b%2 == 1){x=(x*y);if(x>m) x%=m;}y = (y*y);if(y>m) y%=m;b /= 2;}return x%m;}
LL gcd(LL a,LL b){if(b==0) return a; else return gcd(b,a%b);}
LL gen(LL start,LL end){LL diff = end-start;LL temp = rand()%start;return temp+diff;}
using namespace std;
string a , b;
set<int> s;
int main()
{
ios_base::sync_with_stdio(0);
int n , q;
cin >> n >> q;
cin >> a >> b;
int idx = -1;
for(int i = 0; i < a.length() ; i++)
{
if(a[i] == '1' && b[i] == '0')
{
s.insert(i + 1);

}
else if(a[i] == '0' && b[i] == '1')
break;
}
int flag = 0;

while(q--)
{
int idxs;
cin >> idxs;
if(s.find(idxs) != s.end())
s.erase(idxs);
if(s.size() == 0)
{
flag = 1;
}
if(flag)
cout << "YES\n";
else
cout << "NO\n";
}
}


Post a Comment

0 Comments