Header Ad

HackerEarth String Division problem solution

In this HackerEarth String Division problem solution Kevin has a string S consisting of N lowercase English letters.

Kevin wants to split it into 4 pairwise different non-empty parts. For example, string "happynewyear" can be splitted into "happy", "new", "ye" and "ar". He can't delete any characters or change the order of the characters.

Help Kevin and find if there exist at least one possible spliting.


HackerEarth String Division problem solution


HackerEarth String Division problem solution.

#include <string>
#include <vector>
#include <map>
#include <list>
#include <iterator>
#include <set>
#include <queue>
#include <iostream>
#include <sstream>
#include <stack>
#include <deque>
#include <cmath>
#include <memory.h>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <utility>
#include <cassert>
#include<complex>
#include <time.h>
using namespace std;

#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, b, a) for(int i=(b)-1;i>=(a);--i)
#define FILL(A,value) memset(A,value,sizeof(A))

#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define PB push_back
#define MP make_pair
#define Pi 3.14159265358979
#define x0 ikjnrmthklmnt
#define y0 lkrjhkltr
#define y1 ewrgrg

typedef long long Int;
typedef unsigned long long UInt;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef complex<double> base;

const int INF = 1000000000;
const int MAX = 1000007;
const int MAXE = 5000;
const int MAXV = 20*20;
const int BASE = 1000000007;
const int MOD = 1000000007;



int main()
{
//freopen("in.txt", "r", stdin);

int t;
cin >> t;
FOR(tt,0,t)
{
string s;
cin >> s;
if (s.size() >= 10)
{
cout << "YES" << endl;
continue;
}
bool ok = 0;
FOR(i,1,s.size())
FOR(j,i + 1 , s.size())
FOR(k,j + 1 , s.size())
{
string s1 = s.substr(0,i);
string s2 = s.substr(i, j - i);
string s3 = s.substr(j, k - j);
string s4 = s.substr(k, s.size() - k);
if (s1 != s2 && s1 != s3 && s1 != s4 && s2 != s3 && s2 != s4 && s3 != s4)
{
ok = 1;
}
}
if (ok) cout << "YES" << endl;
else cout << "NO" << endl;
}

return 0;
}

Second solution

#include <cstdio>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cassert>
#include <string>
#include <string.h>
#include <cmath>
#include <set>
#include <map>
#include <bitset>
#include <iomanip>

#define X first
#define Y second
#define mp make_pair
#define pb push_back

typedef long long ll;

using namespace std;

void solve() {
string s;
cin>>s;
assert(s.length() <= 1000);
if (s.length() >= 10) {
cout<<"YES"<<endl;
return ;
}
for (int i = 1; i < s.length(); i++) {
for (int j = 1; j < s.length(); j++) {
for (int k = 1; k < s.length(); k++) {
for (int l = 1; l < s.length(); l++) {
string a, b, c, d;
a = s.substr(0, i);
b = s.substr(i, j);
c = s.substr(i + j, k);
d = s.substr(i + j + k, l);
if (a != b && a != c && a != d && b != c && b != d && c != d) {
cout<<"YES"<<endl;
return ;
}
}
}
}
}

cout<<"NO"<<endl;
}

int main() {

int test;
cin>>test;
assert(1 <= test && test <= 100);
while (test--) {
solve();
}
return 0;
}

Post a Comment

0 Comments