Header Ad

HackerEarth Little Shino and the tournament problem solution

In this HackerEarth Little Shino and the tournament problem solution, Little Shino is interested in the fighting tournaments. Once she went to watch one of the tournaments. There were N fighters and ith fighter will be represented by i. Each fighter has some distinct strength. The rules of the tournament are:

Each fight will have 2 fighters.
In a fight, a fighter with more strength will win.
In one round, 1st fighter will fight against 2nd fighter, 3rd fighter will fight against 4th fighter and so on. If there is an odd number of fighters, the last one will qualify to the next round without fighting.


HackerEarth Little Shino and the tournament problem solution


HackerEarth Little Shino and the tournament problem solution.

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
bool B[30];

int main(int argc, char* argv[])
{
if(argc == 2 or argc == 3)
freopen(argv[1], "r", stdin);
if(argc == 3)
freopen(argv[2], "w", stdout);
string s;
int ans, count, k;
cin >> k >> s;
ans = 0;
for(int i = 0;i < s.length();++i)
{
count = 0;
memset(B, false, sizeof(B));
for(int j = i;j < s.length();++j)
{
if(B[s[j] - 'a'] == false) count++;
B[s[j] - 'a'] = true;
if(count == k)
ans++;
}
}
cout << ans << endl;
return 0;
}


Second solution

#include<bits/stdc++.h>

using namespace std;

int k;
string st;
int cnt[1000];
int ans;

int main(){

cin >> k;
cin >> st;

for (int l = 0; l < st.size(); l++)
{
for (int i = 'a'; i <= 'z'; i++)
{
cnt[i] = 0;
}

int dif = 0;

for (int r = l; r < st.size(); r++)
{
cnt[st[r]]++;
if (cnt[st[r]] == 1)
++dif;
if (dif == k)
++ans;
}
}

cout << ans << endl;

return 0;
}

Post a Comment

0 Comments