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

solution using pairs #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
113 changes: 72 additions & 41 deletions src/1087 - Shortest Subsequence.cpp
Original file line number Diff line number Diff line change
@@ -1,46 +1,77 @@
/*
Problem Name: Shortest Subsequence
Problem Link: https://cses.fi/problemset/task/1087
Author: Sachin Srivastava (mrsac7)

*/


#include<bits/stdc++.h>
using namespace std;

// #define int long long
#define endl '\n'

signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r" , stdin);
freopen("output.txt", "w", stdout);
#endif

string s; cin>>s;
int l = 0, r = 0;
bool a = 0, b = 0, c = 0, d = 0;
int n = s.size();
string ans;
while(l < n) {
a = 0, b = 0, c = 0, d = 0;
a += (s[l] == 'A'); b += (s[l] == 'C');
c += (s[l] == 'G'); d += (s[l] == 'T');
r = l+1;
while(r < n && a+b+c+d != 4) {
a += (s[r] == 'A'); b += (s[r] == 'C');
c += (s[r] == 'G'); d += (s[r] == 'T');
r++;
}
if (a+b+c+d == 4)
ans += s[r-1];
l = r;
}
cerr<<a<<' '<<b<<' '<<c<<' '<<d;
if (a+b+c+d == 4)
ans += 'A';
else if (!a) ans += 'A';
else if (!b) ans += 'C';
else if (!c) ans += 'G';
else if (!d) ans += 'T';
cout<<ans;
}
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define int long long int
#define sz(a) (int)a.size()
#define ff first
#define ss second
#define inf 1e18
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define mod 1000000007
#define pi pair<int, int>
#define ti tuple<int, int, int>
#define all(v) (v).begin(),(v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define set_bits(x) __builtin_popcountll(x)
#define deb(x) cout << "[" << #x << " = " << x << "]"
#define deb2(x, y) cout << "[" << #x << " = " << x << " " << #y << " = " << y << "]"
#define output_vector(a) for(auto x : a) cout << x << " "
#define output_vector2(a) for(auto i : a) { for(auto j : i) cout << j << " "; cout << "\n"; }

void solve() {
string s;
cin >> s;

int n = sz(s);

pi g = {0, 0}, c = {0, 0}, a = {0, 0}, t = {0, 0};

int k = 0;
string ans;
for(int i = 0; i < n; i++) {
if(s[i] == 'G') g.ff++, g.ss = i;
else if(s[i] == 'C') c.ff++, c.ss = i;
else if(s[i] == 'A') a.ff++, a.ss = i;
else if(s[i] == 'T') t.ff++, t.ss = i;
if(g.ff && c.ff && a.ff && t.ff) {
k++;
// deb(i);
int j = max({g.ss, c.ss, a.ss, t.ss});
ans += s[j];
g = c = a = t = {0, 0};
}
}


if(a.ff == 0) ans += "A";
else if(c.ff == 0) ans += "C";
else if(g.ff == 0) ans += "G";
else if(t.ff == 0) ans += "T";
else ans += "A";
cout << ans << "\n";
}

int32_t main() {

fastio;

int test_cases = 1;
// cin >> test_cases;

while(test_cases--) {
solve();
}

cerr << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n ";

return 0;
}