Skip to content

Commit

Permalink
Geo Template Added
Browse files Browse the repository at this point in the history
  • Loading branch information
zarif98sjs committed Jul 10, 2020
1 parent c8ff0bd commit b9c9413
Show file tree
Hide file tree
Showing 12 changed files with 585 additions and 1,710 deletions.
11 changes: 11 additions & 0 deletions Cheat Sheet/Game Theory/Nim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Nim

## Optimal Strategy

> “If both A and B play optimally (i.e- they don’t make any mistakes), then the player starting first is guaranteed to win if the Nim-Sum at the beginning of the game is non-zero. Otherwise, if the Nim-Sum evaluates to zero, then player A will lose definitely.”
### Reasoning
Couple of deductions about bitwise XOR necessary for understanding the Optimal Strategy :
- If the XOR sum of ‘n’ numbers is already zero then there is no possibility to make the XOR sum zero by single reduction of a number.
- If the XOR sum of ‘n’ numbers is non-zero then there is at least a single approach by which if you reduce a number, the XOR sum is zero.

Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ using namespace std;
#define MP make_pair
#define F first
#define S second
#define INF INT_MAX

#define ALL(x) (x).begin(), (x).end()
#define DBG(x) cout << __LINE__ << " says: " << #x << " = " << (x) << endl
#define DBG(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl
#define READ freopen("alu.txt", "r", stdin)
#define WRITE freopen("vorta.txt", "w", stdout)

Expand Down Expand Up @@ -62,38 +63,24 @@ string to_str(T x)
//
//}

bool isCorrectBracket(string s)
{
vector<char>v;

for(int i=0;i<(int)s.size();i++)
{
if(s[i]=='(') v.push_back(s[i]);

if(v.empty()) return false;

if(s[i]==')') v.pop_back();

// cout<<v<<endl;
}

return v.empty();
}

int main()
{
//freopen("out.txt","w",stdout);

optimizeIO();

int tc;
cin>>tc;
multiset<int> a {1, 1 ,3, 7, 10, 10 , 13, 20 , 20 , 20};
multiset<int> b {2, 3, 5, 7, 7, 11, 11, 11, 13, 15, 19};

while(tc--)
{
string s;
cin>>s;
vector<int> v;
merge(a.begin(), a.end(), b.begin(), b.end(), back_inserter(v));

cout<<isCorrectBracket(s)<<endl;
}
multiset<int> m(v.begin(), v.end());

for(const auto &element : m) {
cout << element << " ";
}
cout << endl;

return 0;
}
Expand Down Expand Up @@ -133,3 +120,4 @@ ostream &operator <<(ostream &os, set<T>&v)
}



Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@

/**
KMP
NOT KNOWING THIS MULTISET VARIENT OF PBDS COST A PROBLEM IN ICPC PRELI 2019 :)
Idea :
Policy Based Data Structure (MULTI SET)
===========================
Let , s = acbabab
and rs = bababca (reverse of string s)
SEE PBDS SET FOR AVAILABLE FUNCTIONS
How can we make Palindrome of shortest length ?
Implementation 2 : using "pair<,>" in declaration
--------------------------------------------------
acbabab
--bababca
This uses a dummy second variable to use this PBDS as a multiset.
Works fine with almost all the functions .
So , We need to know the Longest Prefix of rs in s .
We can do that using Prefix Function , KMP .
**/

/** Which of the favors of your Lord will you deny ? **/
/**Which of the favors of your Lord will you deny ?**/

//#undef _GLIBCXX_DEBUG

#include<bits/stdc++.h>
using namespace std;
Expand All @@ -29,17 +30,21 @@ using namespace std;
#define MP make_pair
#define F first
#define S second
#define INF INT_MAX

#define ALL(x) (x).begin(), (x).end()
#define DBG(x) cout << __LINE__ << " says: " << #x << " = " << (x) << endl
#define DBG(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl
#define READ freopen("alu.txt", "r", stdin)
#define WRITE freopen("vorta.txt", "w", stdout)

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

template<class TIn>using indexed_set = tree<TIn, null_type, less<TIn>,rb_tree_tag, tree_order_statistics_node_update>;
template<class TIn>
using indexed_multiset = tree<
pair<TIn,TIn>, null_type, less< pair<TIn,TIn> >,
rb_tree_tag, tree_order_statistics_node_update>;

/**
Expand All @@ -59,13 +64,14 @@ ostream &operator <<(ostream &os, vector<T>&v);
template <class T>
ostream &operator <<(ostream &os, set<T>&v);


inline void optimizeIO()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}

const int nmax = 2e5+7;
const int nmax = 1e6+7;
const LL LINF = 1e17;

template <class T>
Expand All @@ -76,64 +82,69 @@ string to_str(T x)
return ss.str();
}

//bool cmp(const PII &A,const PII &B)
//{
//
//}

vector<int> prefix_function(string s) {
int n = (int)s.length();
vector<int> pi(n);
for (int i = 1; i < n; i++) {
int j = pi[i-1];
while (j > 0 && s[i] != s[j])
j = pi[j-1];
if (s[i] == s[j])
j++;
pi[i] = j;
}
return pi;
}
indexed_multiset<int> MS;
indexed_multiset<int> MS2;

int minimumPalindromeLength(string s)
template <class T>
void erase_all_occurrence(T val) /** O(n) **/
{
int sz = s.size();
string rs = s;
reverse(ALL(rs));

vector<int>pi = prefix_function(rs+"#"+s);
int psz = pi.size();
while((*MS.lower_bound({val,0})).F==val)
MS.erase(MS.lower_bound({val,0}));
}

int maximum_match = pi[psz-1];
template <class T>
void erase_one_occurrence(T val) /** O(logN) **/
{
MS.erase(MS.lower_bound({val,0}));
}

int ans = sz + (sz-maximum_match);
return ans;
void printMS()
{
for(auto x:MS)
cout<<x<<" ";
cout<<endl;
}

int main()
{
optimizeIO();

int tc;
cin>>tc;
MS.clear();

for(int qq=1;qq<=tc;qq++)
int n;
cin>>n;

for(int i=1;i<=n;i++)
{
string s;
cin>>s;
int x;
cin>>x;

int ans = minimumPalindromeLength(s);
MS.insert({x,i}); /** insert **/
MS2.insert({x,i}); /** insert **/
}

printMS();

vector<int> v;
merge(MS.begin(), MS.end(), MS2.begin(), MS2.end(), back_inserter(v));

multiset<int> m(v.begin(), v.end());

for(const auto &element : m) {
cout << element << " ";
}
cout << endl;

cout<<"Case "<<qq<<": ";
cout<<ans<<endl;

}

return 0;
}


/**
8
30 10 20 10 40 30 20 20
**/

template<class T1, class T2>
Expand Down Expand Up @@ -165,5 +176,3 @@ ostream &operator <<(ostream &os, set<T>&v)
os<<" ]";
return os;
}


Loading

0 comments on commit b9c9413

Please sign in to comment.