Skip to content

Commit

Permalink
added some topcoder problems
Browse files Browse the repository at this point in the history
  • Loading branch information
rubabredwan committed Nov 7, 2016
1 parent a8e2fd6 commit 52f579b
Show file tree
Hide file tree
Showing 6 changed files with 452 additions and 0 deletions.
96 changes: 96 additions & 0 deletions Topcoder/BearPasswordLexic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* Bismillahir Rahmanir Rahim */

#include <bits/stdc++.h>

#define rep(i, n) for(int i=0;i<n;i++)
#define repn(i, n) for(int i=1;i<=n;i++)
#define set(i, n) memset(i, n, sizeof(i))

#define mx 100005
#define inf 1e9
#define pb push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

struct BearPasswordLexic{
int n;
string findPassword(vector<int>x){
n = x.size();
string ret = "";
int p = 1;
vector<int>aa, bb;
vector<int>vec;
while(1){
bool flag = false;
for(int i=n-1;i>=0;i--){
if(x[i] > 0){
flag = true;
int cur = 1;
vec.pb(i+1);
for(int j=i;j>=0;j--){
x[j] -= cur;
cur += 1;
}
break;
}
}
if(flag == false) break;
p ^= 1;
}
for(auto u : x) if(u != 0) return "";

sort(vec.begin(), vec.end());

int fk = vec.size() / 2;

for(int i=0;i<fk;i++) bb.pb(vec[i]);
for(int i=fk;i<vec.size();i++) aa.pb(vec[i]);

sort(aa.begin(), aa.end());
reverse(aa.begin(), aa.end());
sort(bb.begin(), bb.end());
if((aa.size() - bb.size()) > 1) return "";
int sum = 0;
for(auto u : aa) sum += u;
for(auto u : bb) sum += u;
if(sum != x.size()) return "";

for(int i=0;i<aa.size()+bb.size();i++){
if(i % 2 == 0){
rep(j, aa[i/2]) ret += "a";
}
else{
rep(j, bb[i/2]) ret += "b";
}
}

return ret;
}

// BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); }
private:
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
void verify_case(int Case, const string &Expected, const string &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
void test_case_0() { int Arr0[] = {5,0,0,0,0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "ababa"; verify_case(0, Arg1, findPassword(Arg0)); }
void test_case_1() { int Arr0[] = {4,2,1,0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "aaab"; verify_case(1, Arg1, findPassword(Arg0)); }
void test_case_2() { int Arr0[] = {3,1,1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = ""; verify_case(2, Arg1, findPassword(Arg0)); }
void test_case_3() { int Arr0[] = {4,3,2,1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = "aaaa"; verify_case(3, Arg1, findPassword(Arg0)); }
void test_case_4() { int Arr0[] = {0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = ""; verify_case(4, Arg1, findPassword(Arg0)); }
void test_case_5() { int Arr0[] = {4,0,3,2}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arg1 = ""; verify_case(5, Arg1, findPassword(Arg0)); }

// END CUT HERE

};

// BEGIN CUT HERE
int main(){
BearPasswordLexic ___test;
___test.run_test(-1);
return 0;
}
// END CUT HERE
121 changes: 121 additions & 0 deletions Topcoder/BiconnectedDiv1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/* Bismillahir Rahmanir Rahim */

#include <bits/stdc++.h>

#define REP(i, n) for(int i=0;i<n;i++)
#define repn(i, n) for(int i=1;i<=n;i++)
#define set(i, n) memset(i, n, sizeof(i))

#define pb push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef long long int64;

const int
MAXN = 502,
MAXE = 100000,
oo = (int)1e9;

int T, V, L, E;
int src, snk, sol;
int cant[MAXN];
int best[MAXN];
int from[MAXN];
int head[MAXN];
struct edge {
int u, v, w, cap, next;
} edges[MAXE];

void add_edge( int u, int v, int w, int cap ) {
edges[E++] = (edge){ u, v, +w, cap, head[u] };
head[u] = E - 1;
edges[E++] = (edge){ v, u, -w, 0, head[v] };
head[v] = E - 1;
}

bool augment( int src, int snk ) {

fill( best, best + V + 2, oo );
best[src] = 0;

queue< int > Q;
for ( Q.push( src ); !Q.empty(); Q.pop() ) {
int u = Q.front();
for ( int e = head[u]; e != -1; e = edges[e].next ) {
int v = edges[e].v;
if ( edges[e].cap && best[u] + edges[e].w < best[v] ) {
Q.push( v );
best[v] = best[u] + edges[e].w;
from[v] = e;
}
}
}

if ( best[snk] == oo )
return false;

sol += best[snk];
for ( int x = snk; x != src; x = edges[ from[x] ].u ) {
edges[ from[x] ].cap--;
edges[ from[x]^1 ].cap++;
}

return true;
}


struct BiconnectedDiv1{
int minimize(vector <int> w1, vector <int> w2){
E = 0;
memset( head, -1, sizeof( head ) );
memset( cant, 0, sizeof( cant ) );
V = w1.size() + 3;
int tot = 0;
for(int i=0;i<w1.size();i++){
add_edge(i, i+1, w1[i], 1);
tot += w1[i];
}
for(int i=0;i<w2.size();i++){
add_edge(i, i+2, w2[i], 1);
tot += w2[i];
}

src = V;
snk = V + 1;

add_edge(src, 0, 0, 2);
add_edge(w1.size(), snk, 0, 2);

sol = 0;

REP(i, 2) augment(src, snk);

return sol;

}

// BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); }
private:
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
void test_case_0() { int Arr0[] = {1,2}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {3}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 6; verify_case(0, Arg2, minimize(Arg0, Arg1)); }
void test_case_1() { int Arr0[] = {3,0,4}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1,2}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 10; verify_case(1, Arg2, minimize(Arg0, Arg1)); }
void test_case_2() { int Arr0[] = {3,3,3,3}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {3,6,3}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 18; verify_case(2, Arg2, minimize(Arg0, Arg1)); }
void test_case_3() { int Arr0[] = {7243,7525,8467,6351,9453,8456,8175,5874,6869,7400,6438,8926,6876}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {2642,1743,3546,4100,2788,3019,2678,1935,1790,2674,3775,1920}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 46729; verify_case(3, Arg2, minimize(Arg0, Arg1)); }

// END CUT HERE

};

// BEGIN CUT HERE
int main(){
BiconnectedDiv1 ___test;
___test.run_test(-1);
return 0;
}
// END CUT HERE
62 changes: 62 additions & 0 deletions Topcoder/Gperm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* Bismillahir Rahmanir Rahim */

#include <bits/stdc++.h>

#define rep(i, n) for(int i=0;i<n;i++)
#define repn(i, n) for(int i=1;i<=n;i++)
#define set(i, n) memset(i, n, sizeof(i))

#define mx 55
#define inf 1e9
#define pb push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

struct Gperm{
vector<int>edge[mx];
int done[mx];
int countfee(vector <int> x, vector <int> y){
int ret = inf, n = 50, m = x.size();
for(int mask = 0; mask < (1 << m);mask++){
vector<int>f(n);
rep(i, m){
if(mask & (1 << i)) f[y[i]]++;
else f[x[i]]++;
}
sort(f.begin(), f.end());
int ans = 0;
for(int i=0;i<n;i++){
ans += f[i] * (n - i);
}
ret = min(ret, ans);
}
return ret;
}

// BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); }
private:
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
void test_case_0() { int Arr0[] = {0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 1; verify_case(0, Arg2, countfee(Arg0, Arg1)); }
void test_case_1() { int Arr0[] = {0,1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1,2}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 2; verify_case(1, Arg2, countfee(Arg0, Arg1)); }
void test_case_2() { int Arr0[] = {4,7,7}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {7,4,4}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 3; verify_case(2, Arg2, countfee(Arg0, Arg1)); }
void test_case_3() { int Arr0[] = {0,0,1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {1,2,2}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 4; verify_case(3, Arg2, countfee(Arg0, Arg1)); }
void test_case_4() { int Arr0[] = {7,8,9}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {4,5,6}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 6; verify_case(4, Arg2, countfee(Arg0, Arg1)); }
void test_case_5() { int Arr0[] = {45,28,42,5,27,27,42,36,14,27,19,42,24,27,8,31,24,27,14,28}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {45,27,45,8,34,34,28,0,11,42,24,19,14,31,45,42,14,24,28,27}; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 53; verify_case(5, Arg2, countfee(Arg0, Arg1)); }

// END CUT HERE

};

// BEGIN CUT HERE
int main(){
Gperm ___test;
___test.run_test(-1);
return 0;
}
// END CUT HERE
48 changes: 48 additions & 0 deletions Topcoder/SortingSubsets.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* Bismillahir Rahmanir Rahim */

#include <bits/stdc++.h>

#define rep(i, n) for(int i=0;i<n;i++)
#define repn(i, n) for(int i=1;i<=n;i++)
#define set(i, n) memset(i, n, sizeof(i))

#define mx 100005
#define inf 1e9
#define pb push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

struct SortingSubsets{
int getMinimalSize(vector <int> a){
int ret = 0;
vector<int>f = a;
sort(f.begin(), f.end());
rep(i, a.size()) if(a[i] != f[i]) ret++;
return ret;
}

// BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); }
private:
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
void test_case_0() { int Arr0[] = {3, 2, 1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; verify_case(0, Arg1, getMinimalSize(Arg0)); }
void test_case_1() { int Arr0[] = {1, 2, 3, 4}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 0; verify_case(1, Arg1, getMinimalSize(Arg0)); }
void test_case_2() { int Arr0[] = {4, 4, 4, 3, 3, 3}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 6; verify_case(2, Arg1, getMinimalSize(Arg0)); }
void test_case_3() { int Arr0[] = {11, 11, 49, 7, 11, 11, 7, 7, 11, 49, 11}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 7; verify_case(3, Arg1, getMinimalSize(Arg0)); }

// END CUT HERE

};

// BEGIN CUT HERE
int main(){
SortingSubsets ___test;
___test.run_test(-1);
return 0;
}
// END CUT HERE
65 changes: 65 additions & 0 deletions Topcoder/SquareFreeString.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* Bismillahir Rahmanir Rahim */

#include <bits/stdc++.h>

#define rep(i, n) for(int i=0;i<n;i++)
#define repn(i, n) for(int i=1;i<=n;i++)
#define set(i, n) memset(i, n, sizeof(i))

#define mx 100005
#define inf 1e9
#define pb push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

struct SquareFreeString{

bool ok(string s){
if(s.size() % 2) return false;
int n = s.size();
n /= 2;
for(int i=0, j=n;j<s.size();i++, j++){
if(s[i] != s[j]) return false;
}
return true;
}

string isSquareFree(string s){
string ret;
int n = s.size();
for(int i=0;i<n;i++){
string str = "";
for(int j=i;j<n;j++){
str += s[j];
if(ok(str)) return "not square-free";
}
}
return "square-free";
}

// BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }
private:
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
void verify_case(int Case, const string &Expected, const string &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
void test_case_0() { string Arg0 = "bobo"; string Arg1 = "not square-free"; verify_case(0, Arg1, isSquareFree(Arg0)); }
void test_case_1() { string Arg0 = "apple"; string Arg1 = "not square-free"; verify_case(1, Arg1, isSquareFree(Arg0)); }
void test_case_2() { string Arg0 = "pen"; string Arg1 = "square-free"; verify_case(2, Arg1, isSquareFree(Arg0)); }
void test_case_3() { string Arg0 = "aydyamrbnauhftmphyrooyq"; string Arg1 = "not square-free"; verify_case(3, Arg1, isSquareFree(Arg0)); }
void test_case_4() { string Arg0 = "qwertyuiopasdfghjklzxcvbnm"; string Arg1 = "square-free"; verify_case(4, Arg1, isSquareFree(Arg0)); }

// END CUT HERE

};

// BEGIN CUT HERE
int main(){
SquareFreeString ___test;
___test.run_test(-1);
return 0;
}
// END CUT HERE
Loading

0 comments on commit 52f579b

Please sign in to comment.