Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smallest KMP AUG2020B solution #122

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions Codechef-2020/APRIL20B/sqrdsub.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
profile link - https://www.codechef.com/users/harsh_gupta_33
solution link - https://www.codechef.com/viewsolution/31679673
*/
#include<bits/stdc++.h>
using namespace std;
long long int lcnt[100001], rcnt[100001];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--)
{
long long int n,i;
long long int sum=0,ans;
cin>>n;
int odd=0;
long long int b[n+1];
long long int a[n+1];
memset(lcnt,0,sizeof(lcnt));
memset(rcnt,0,sizeof(rcnt));
for(i=0;i<n;i++)
{
cin>>a[i];
if(a[i]%2==0 && a[i]%4!=0)
b[i]=1;
else{
b[i]=0;
}
}
for(i=0;i<n;i++)
{
if(b[i]==0 && a[i]%2!=0)
odd++;
else if(b[i]==1){
lcnt[i]=odd;
odd=0;
}
else
odd=0;
}
odd=0;
for(i=n-1;i>=0;i--)
{
if(b[i]==0 && a[i]%2!=0)
odd++;
else if(b[i]==1){
rcnt[i]=odd;
odd=0;
}
else
odd=0;
}
for(i=0;i<n;i++){
if(b[i]==1)
sum+=(lcnt[i]+1)*(rcnt[i]+1);
}
long long int tot=(n*(n+1))/2;
ans=tot-sum;
cout<<ans<<"\n";
}
}
106 changes: 106 additions & 0 deletions Codechef-2020/AUG20B/skmp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
profile link - https://www.codechef.com/users/harsh_gupta_33
problem link - https://www.codechef.com/AUG20B/problems/SKMP
solution link - https://www.codechef.com/viewsolution/36307112
*/
#include<bits/stdc++.h>
using namespace std;
#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define MAX 3001
#define ll long long
#define pb push_back
#define ff first
#define ss second
const int mod = 1000000007;

//void swap(int *x,int *y){int temp=*x;*x=*y;*y=temp; }
// ll int binpow(ll int a,ll int b){ll int ans=1; while(b>0){if(b&1){ans=ans*a;}a=a*a;b>>=1;}return ans;}
//ll power(ll a,ll b,ll m) { ll ans=1; a=a%m; while(b) { if(b&1) ans=(ans*a)%m; b/=2; a=(a*a)%m; } return ans; }
//ll modInverse(ll a, ll m) {return power(a, m-2, m);}
//bool is_prime(int n) { if (n < 2)return false; for (int i = 2; i * i <= n; i++) {if (n % i == 0) return false;} return true;}
//void sieve(int n){ bool prime[n+1]; memset(prime,true,sizeof(prime));prime[0]=false;prime[1]=false; for(int i=2;i*i<=n;i++){ if(prime[i]){for(int j=i*i;j<=n;j=j+i)prime[j]=false;}}for(int i=0;i<=n;i++){if(prime[i])cout<<i<<endl;}}

int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t;
cin >> t;
while (t--) {
string s, p, temp;
cin >> s >> p;
temp = p;
int size = s.size(), check[size] = {0};
int k = temp.size() - 1;
sort(temp.begin(), temp.end());
sort(s.begin(), s.end());
if (p.size() > 1)
{
int flag = 0;
for (int i = 1; i < p.size(); i++)
{
if (p[i] < p[0])
{
flag = 1;
break;
}
else if (p[i] > p[0])
break;
}
if (flag)
{
k = 0;
for (int i = 0; i < size ; i++)
{
if (k < temp.size() && s[i] == temp[k])
{
check[i] = 1;
k++;
}
}
}
else
{
k = temp.size() - 1;
for (int i = size - 1; i >= 0; i--)
{
if (k >= 0 && s[i] == temp[k])
{
check[i] = 1;
k--;
}
}
}
// cout << s << endl;
// for (int i = 0; i < size; i++)
// cout << check[i];
// cout << endl;
for (int i = 0; i < size; i++)
{
if (check[i] == 0)
cout << s[i];
else
{
if (s[i] == p[0] && check[i] == 1)
{
k = i;
cout << p;
break;
}

}
}
for (int i = k + 1; i < size; i++)
{
if (check[i] == 0)
cout << s[i];
}
}
else
cout << s;
cout << endl;
}

}