From b9c9413575db2fcafeb58bcf8fbe429148f74c73 Mon Sep 17 00:00:00 2001 From: "Md. Zarif Ul Alam" Date: Fri, 10 Jul 2020 21:49:59 +0600 Subject: [PATCH] Geo Template Added --- Cheat Sheet/Game Theory/Nim.md | 11 + .../test.cpp | 42 +-- .../test2.cpp | 115 ++++--- .../02 Geometry Template (Point & Line).cpp | 316 ++++++++++++++++++ .../03 Geometry Template (Rectangle).cpp | 181 ++++++++++ ...orrect Bracket Sequence (Segment Tree).cpp | 222 ------------ .../00 Substring Search [Suffix Array].cpp | 263 --------------- .../01 Counting Substrings [Suffix Array].cpp | 296 ---------------- .../String/02 Counting Substrings [KMP].cpp | 152 --------- ...of Different Substrings [Suffix Array].cpp | 249 -------------- ...Substring of two string [Suffix Array].cpp | 274 --------------- ...er of occurrences of each prefix [KMP].cpp | 174 ---------- 12 files changed, 585 insertions(+), 1710 deletions(-) create mode 100644 Cheat Sheet/Game Theory/Nim.md rename Miscellaneous/Problems/Popular/00 Correct Bracket Sequence.cpp => Data Structure/test.cpp (80%) rename Miscellaneous/Problems/String/06 Make Palindrome adding minimum characters [KMP].cpp => Data Structure/test2.cpp (50%) create mode 100644 Miscellaneous/02 Geometry Template (Point & Line).cpp create mode 100644 Miscellaneous/03 Geometry Template (Rectangle).cpp delete mode 100644 Miscellaneous/Problems/Popular/01 Correct Bracket Sequence (Segment Tree).cpp delete mode 100644 Miscellaneous/Problems/String/00 Substring Search [Suffix Array].cpp delete mode 100644 Miscellaneous/Problems/String/01 Counting Substrings [Suffix Array].cpp delete mode 100644 Miscellaneous/Problems/String/02 Counting Substrings [KMP].cpp delete mode 100644 Miscellaneous/Problems/String/03 Number of Different Substrings [Suffix Array].cpp delete mode 100644 Miscellaneous/Problems/String/04 Longest Common Substring of two string [Suffix Array].cpp delete mode 100644 Miscellaneous/Problems/String/05 Counting the number of occurrences of each prefix [KMP].cpp diff --git a/Cheat Sheet/Game Theory/Nim.md b/Cheat Sheet/Game Theory/Nim.md new file mode 100644 index 0000000..5a33a98 --- /dev/null +++ b/Cheat Sheet/Game Theory/Nim.md @@ -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. + diff --git a/Miscellaneous/Problems/Popular/00 Correct Bracket Sequence.cpp b/Data Structure/test.cpp similarity index 80% rename from Miscellaneous/Problems/Popular/00 Correct Bracket Sequence.cpp rename to Data Structure/test.cpp index af8d549..016c299 100644 --- a/Miscellaneous/Problems/Popular/00 Correct Bracket Sequence.cpp +++ b/Data Structure/test.cpp @@ -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) @@ -62,38 +63,24 @@ string to_str(T x) // //} -bool isCorrectBracket(string s) -{ - vectorv; - - 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<>tc; + multiset a {1, 1 ,3, 7, 10, 10 , 13, 20 , 20 , 20}; + multiset b {2, 3, 5, 7, 7, 11, 11, 11, 13, 15, 19}; - while(tc--) - { - string s; - cin>>s; + vector v; + merge(a.begin(), a.end(), b.begin(), b.end(), back_inserter(v)); - cout< m(v.begin(), v.end()); + + for(const auto &element : m) { + cout << element << " "; + } + cout << endl; return 0; } @@ -133,3 +120,4 @@ ostream &operator <<(ostream &os, set&v) } + diff --git a/Miscellaneous/Problems/String/06 Make Palindrome adding minimum characters [KMP].cpp b/Data Structure/test2.cpp similarity index 50% rename from Miscellaneous/Problems/String/06 Make Palindrome adding minimum characters [KMP].cpp rename to Data Structure/test2.cpp index 5e77357..d287933 100644 --- a/Miscellaneous/Problems/String/06 Make Palindrome adding minimum characters [KMP].cpp +++ b/Data Structure/test2.cpp @@ -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 using namespace std; @@ -29,9 +30,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) @@ -39,7 +41,10 @@ using namespace std; #include using namespace __gnu_pbds; -templateusing indexed_set = tree,rb_tree_tag, tree_order_statistics_node_update>; +template +using indexed_multiset = tree< + pair, null_type, less< pair >, + rb_tree_tag, tree_order_statistics_node_update>; /** @@ -59,13 +64,14 @@ ostream &operator <<(ostream &os, vector&v); template ostream &operator <<(ostream &os, set&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 @@ -76,64 +82,69 @@ string to_str(T x) return ss.str(); } -//bool cmp(const PII &A,const PII &B) -//{ -// -//} - -vector prefix_function(string s) { - int n = (int)s.length(); - vector 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 MS; +indexed_multiset MS2; -int minimumPalindromeLength(string s) +template +void erase_all_occurrence(T val) /** O(n) **/ { - int sz = s.size(); - string rs = s; - reverse(ALL(rs)); - - vectorpi = 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 +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<>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 v; + merge(MS.begin(), MS.end(), MS2.begin(), MS2.end(), back_inserter(v)); + + multiset m(v.begin(), v.end()); + + for(const auto &element : m) { + cout << element << " "; + } + cout << endl; - cout<<"Case "< @@ -165,5 +176,3 @@ ostream &operator <<(ostream &os, set&v) os<<" ]"; return os; } - - diff --git a/Miscellaneous/02 Geometry Template (Point & Line).cpp b/Miscellaneous/02 Geometry Template (Point & Line).cpp new file mode 100644 index 0000000..8126aeb --- /dev/null +++ b/Miscellaneous/02 Geometry Template (Point & Line).cpp @@ -0,0 +1,316 @@ + +/** + +STRUCTURE +--------- +1. PT +2. LINE + +FUNCTION OVERVIEW +----------------- + +double dot(PT p, PT q) +double cross(PT p, PT q) +double dist2(PT p, PT q) + +PT RotateCCW90(PT p) +PT RotateCW90(PT p) +PT RotateCCW(PT p, double t) + +PT LineIntersectionUsingPoint(PT a, PT b, PT c, PT d) +PT LineIntersectionUsingLine(LINE l1, LINE l2) + +PT Middle(PT p, PT q) + +LINE points_to_line(PT p, PT q) +LINE perpendicular(LINE l, PT p) + +PT ComputeCircleCenter(PT a, PT b, PT c) + +bool LinesParallel(PT a, PT b, PT c, PT d) +bool LinesCollinear(PT a, PT b, PT c, PT d) + +PT ProjectPointLine(PT a, PT b, PT c) +PT ProjectPointSegment(PT a, PT b, PT c) +double DistancePointSegment(PT a, PT b, PT c) +double DistancePointLine(PT a, PT b, PT c) + +double HeronTriangleArea(double a,double b,double c) +double PassPointThroughLine(PT a,LINE l) + +**/ + + +/** Which of the favors of your Lord will you deny ? **/ + +#include +using namespace std; + +#define LL long long +#define PII pair +#define PLL pair +#define F first +#define S second + +#define DBL long long +//#define DBL double + +#define ALL(x) (x).begin(), (x).end() +#define READ freopen("alu.txt", "r", stdin) +#define WRITE freopen("vorta.txt", "w", stdout) + +#ifndef ONLINE_JUDGE +#define DBG(x) cout << __LINE__ << " says: " << #x << " = " << (x) << endl +#else +#define DBG(x) +#endif + +template +ostream &operator <<(ostream &os, pair&p); +template +ostream &operator <<(ostream &os, vector&v); +template +ostream &operator <<(ostream &os, set&v); + +inline void optimizeIO() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); +} + +const int nmax = 2e5+7; + +double INF = 1e100; +double EPS = 1e-12; + +const double eps = 1e-11; +int cmp(double x, double y) +{ + return (x <= y + eps) ? (x + eps < y) ? -1 : 0 : 1; /// 0 -> x equal y , 1 -> x>y , -1 -> x EPS && dot(d, d) > EPS); + return a + b*cross(c, d)/cross(b, d); +} + +PT LineIntersectionUsingLine(LINE l1, LINE l2) +{ + PT p; + + p.y = (l2.c * l1.a - l1.c * l2.a) / (l1.b * l2.a - l2.b * l1.a); + p.x = (l2.c * l1.b - l1.c * l2.b) / (l1.a * l2.b - l2.a * l1.b); + + return p; +} + +PT Middle(PT p, PT q) +{ + PT m = { (p.x + q.x) / 2, (p.y + q.y) / 2 }; + + return m; +} + +LINE points_to_line(PT p, PT q) +{ + LINE l; + + l.a = p.y - q.y; + l.b = q.x - p.x; + l.c = -l.a * p.x - l.b * p.y; + + return l; +} + +LINE perpendicular(LINE l, PT p) +{ + LINE per; + + per.a = -l.b; + per.b = l.a; + per.c = -per.a * p.x - per.b * p.y; + + return per; +} + +PT ComputeCircleCenter(PT a, PT b, PT c) +{ + b=(a+b)/2; + c=(a+c)/2; + return LineIntersectionUsingPoint(b, b+RotateCW90(a-b), c, c+RotateCW90(a-c)); +} + + +bool LinesParallel(PT a, PT b, PT c, PT d) +{ + return fabs(cross(b-a, c-d)) < EPS; +} + +bool LinesCollinear(PT a, PT b, PT c, PT d) +{ + return LinesParallel(a, b, c, d) + && fabs(cross(a-b, a-c)) < EPS + && fabs(cross(c-d, c-a)) < EPS; +} + +/// project point c onto line through a and b +/// assuming a != b +PT ProjectPointLine(PT a, PT b, PT c) +{ + return a + (b-a)*dot(c-a, b-a)/dot(b-a, b-a); +} + +/// project point c onto line segment through a and b +PT ProjectPointSegment(PT a, PT b, PT c) +{ + double r = dot(b-a,b-a); + if (fabs(r) < EPS) + return a; + r = dot(c-a, b-a)/r; + if (r < 0) + return a; + if (r > 1) + return b; + return a + (b-a)*r; +} + +/// compute distance from c to segment between a and b +double DistancePointSegment(PT a, PT b, PT c) +{ + return sqrt(dist2(c, ProjectPointSegment(a, b, c))); +} + +/// compute distance from c to line between a and b +double DistancePointLine(PT a, PT b, PT c) +{ + return sqrt(dist2(c, ProjectPointLine(a, b, c))); +} + +double HeronTriangleArea(double a,double b,double c) +{ + double s=(a+b+c)*0.5; + double area=sqrt(s*(s-a)*(s-b)*(s-c)); + return area; +} + +double PassPointThroughLine(PT a,LINE l) +{ + return (l.a*a.x+l.b*a.y+l.c); +} + +int main() +{ + optimizeIO(); + + int tc; + cin>>tc; + + while(tc--) + { + + } + + return 0; +} + +/** + +**/ + +template +ostream &operator <<(ostream &os, pair&p) +{ + os<<"{"< +ostream &operator <<(ostream &os, vector&v) +{ + os<<"[ "; + for(int i=0; i +ostream &operator <<(ostream &os, set&v) +{ + os<<"[ "; + for(T i:v) + { + os< +using namespace std; + +#define LL long long +#define PII pair +#define PLL pair +#define F first +#define S second + +#define DBL long long +//#define DBL double + +#define ALL(x) (x).begin(), (x).end() +#define READ freopen("alu.txt", "r", stdin) +#define WRITE freopen("vorta.txt", "w", stdout) + +#ifndef ONLINE_JUDGE +#define DBG(x) cout << __LINE__ << " says: " << #x << " = " << (x) << endl +#else +#define DBG(x) +#endif + +template +ostream &operator <<(ostream &os, pair&p); +template +ostream &operator <<(ostream &os, vector&v); +template +ostream &operator <<(ostream &os, set&v); + +inline void optimizeIO() +{ + ios_base::sync_with_stdio(false); + cin.tie(NULL); +} + +const int nmax = 2e5+7; + +double INF = 1e100; +double EPS = 1e-12; + +struct PT +{ + DBL x,y; + PT() {} + PT(DBL x,DBL y) : x(x), y(y) {} + PT(const PT &p) : x(p.x), y(p.y) {} + + PT operator + (const PT &p) const + { + return PT(x+p.x, y+p.y); + } + PT operator - (const PT &p) const + { + return PT(x-p.x, y-p.y); + } + PT operator * (DBL c) const + { + return PT(x*c, y*c ); + } + PT operator / (DBL c) const + { + return PT(x/c, y/c ); + } +}; + +/** + +Rectangles are defined by two points. +One is the LOWER_LEFT and the other one is the UPPER_RIGHT + +**/ + +/** +Intersecting Points of 2 Rectangle +returns false if No Intersection +**/ +bool intersectingPoints(PT a,PT b,PT c,PT d,PT &na,PT &nb) +{ + na.x = max(a.x,c.x); + na.y = max(a.y,c.y); + + nb.x = min(b.x,d.x); + nb.y = min(b.y,d.y); + + if(na.x>nb.x || na.y>nb.y) + return false; + + return true; +} + + +///Calculates Area of a Rectangle +LL computeArea(PT a,PT b) +{ + return (b.x-a.x) * (b.y-a.y); +} + + +///Calculate the overlapping area of two rectangles. +LL overlapArea(PT a,PT b,PT c,PT d) +{ + /** Check if there is indeed an overlap. + * e.g. c.x>=b.x i.e. the most left point of the rectangle (c,d) is + * on the right side of the most right point of the rectangle (a,b), + * therefore there is no overlapping. + */ + if ( (c.x>=b.x) || (c.y>= b.y) || (a.x>=d.x) || (a.y >= d.y) ) + return 0; + + /** bottom left polong long of the overlapping area. */ + LL bl_x = max(a.x, c.x); + LL bl_y = max(a.y, c.y); + + /** top right polong long of the overlapping area. */ + LL tr_x = min(b.x, d.x); + LL tr_y = min(b.y, d.y); + + return ((tr_x - bl_x) * (tr_y - bl_y)); +} + +/** +Find the total area covered by two rectilinear rectangles in a 2D plane. +Each rectangle is defined by its bottom left corner and top right corner. +**/ +LL computeTotalArea(PT a,PT b,PT c,PT d) +{ + /// The addition of area of the two rectangles minus the overlapping area. + return (computeArea(a,b) + computeArea(c,d) - overlapArea(a,b,c,d)); +} + +int main() +{ + PT a1(2,2) , b1(4,4) , a2(1,1) , b2(3,5) , a3(3,1) , b3(5,5); + + PT c1,d1,c2,d2; + + intersectingPoints(a1,b1,a2,b2,c1,d1); + intersectingPoints(a1,b1,a3,b3,c2,d2); + + cout< +ostream &operator <<(ostream &os, pair&p) +{ + os<<"{"< +ostream &operator <<(ostream &os, vector&v) +{ + os<<"[ "; + for(int i=0; i +ostream &operator <<(ostream &os, set&v) +{ + os<<"[ "; + for(T i:v) + { + os< -using namespace std; - -#define LL long long -#define PII pair -#define PLL pair -#define MP make_pair -#define F first -#define S second - -#define ALL(x) (x).begin(), (x).end() -#define DBG(x) cout << __LINE__ << " says: " << #x << " = " << (x) << endl -#define READ freopen("alu.txt", "r", stdin) -#define WRITE freopen("vorta.txt", "w", stdout) - -#include -#include -using namespace __gnu_pbds; - -templateusing indexed_set = tree,rb_tree_tag, tree_order_statistics_node_update>; - -/** - -PBDS -------------------------------------------------- -1) insert(value) -2) erase(value) -3) order_of_key(value) // 0 based indexing -4) *find_by_order(position) // 0 based indexing - -**/ - -template -ostream &operator <<(ostream &os, pair&p); -template -ostream &operator <<(ostream &os, vector&v); -template -ostream &operator <<(ostream &os, set&v); - -inline void optimizeIO() -{ - ios_base::sync_with_stdio(false); - cin.tie(NULL); -} - -const LL LINF = 1e17; - -template -string to_str(T x) -{ - stringstream ss; - ss<>1; - int lc = cur<<1, rc = lc|1; - - if (updex <= mid) - update(lc, start, mid, updex, value); - else - update(rc, mid+1, end, updex, value); - - Tree[cur].merge_nodes(Tree[lc],Tree[rc]); -} - - -void query(node &res,int cur,int start,int end,int l,int r) -{ - if(start>=l && end<=r) - { - res = Tree[cur]; - return; - } - - int lc = 2*cur, rc = 2*cur+1; - int mid = (start+end)/2; - - if(r<=mid) - query(res,lc,start,mid,l,r); - else if(l>mid) - query(res,rc,mid+1,end,l,r); - else - { - node left,right; - query(left,lc,start,mid,l,r); - query(right,rc,mid+1,end,l,r); - res.merge_nodes(left,right); - } -} - -int main() -{ - optimizeIO(); - - cin>>s; - s = "#" + s; - - n = s.size(); - - build(1,1,n); - - int q; - cin>>q; - - while(q--) - { - int l , r; - cin>>l>>r; - - node res; - query(res,1,1,n,l,r); - - if(res.left > 0 || res.right > 0) cout<<"Not Correct Bracket Sequence"< -ostream &operator <<(ostream &os, pair&p) -{ - os<<"{"< -ostream &operator <<(ostream &os, vector&v) -{ - os<<"[ "; - for(int i=0; i -ostream &operator <<(ostream &os, set&v) -{ - os<<"[ "; - for(T i:v) - { - os< -using namespace std; - -#define LL long long -#define PII pair -#define PLL pair -#define MP make_pair -#define F first -#define S second - -#define ALL(x) (x).begin(), (x).end() -#define DBG(x) cout << __LINE__ << " says: " << #x << " = " << (x) << endl -#define READ freopen("alu.txt", "r", stdin) -#define WRITE freopen("vorta.txt", "w", stdout) - -#include -#include -using namespace __gnu_pbds; - -templateusing indexed_set = tree,rb_tree_tag, tree_order_statistics_node_update>; - -/** - -PBDS -------------------------------------------------- -1) insert(value) -2) erase(value) -3) order_of_key(value) // 0 based indexing -4) *find_by_order(position) // 0 based indexing - -**/ - -template -ostream &operator <<(ostream &os, pair&p); -template -ostream &operator <<(ostream &os, vector&v); -template -ostream &operator <<(ostream &os, set&v); - -inline void optimizeIO() -{ - ios_base::sync_with_stdio(false); - cin.tie(NULL); -} - -const int nmax = 2e5+7; -const LL LINF = 1e17; - -template -string to_str(T x) -{ - stringstream ss; - ss<> &v) -{ - int n = v.size(); - - { - vectorcnt(n); /// for counting sort - - for(auto el:v) - cnt[el.F.S]++; /// first sort according to second element - - vector> nv(n); - vectorpos(n); /// pos[i] = first empty position in bucket i - - pos[0] = 0; - for(int i=1; icnt(n); /// for counting sort - - for(auto el:v) - cnt[el.F.F]++; /// first sort according to second element - - vector> nv(n); - vectorpos(n); /// pos[i] = first empty position in bucket i - - pos[0] = 0; - for(int i=1; i buildSuffixArray(string s) -{ - s += "$"; - - int n = s.size(); - - /// init p , c - - vectorv(n); - - for(int i=0; ip(n), c(n); - for(int i=0; i>v(n); - - for(int i=0; i &ara) -{ - while(lo<=hi) - { - int mid = lo + (hi-lo)/2; - - string chk = s.substr(ara[mid],key.size()); - - if(chkkey) hi = mid - 1; - else return true; - } - - return false; -} - -int main() -{ - optimizeIO(); - - string s; - cin>>s; - - int n = s.size(); - - vectorp = buildSuffixArray(s); - -// for(int i=0; i<(int)p.size(); i++) -// cout<>q; - - while(q--) - { - string sub; - cin>>sub; - - if(binarySearch(0,n,s,sub,p)) cout<<"Yes"< -ostream &operator <<(ostream &os, pair&p) -{ - os<<"{"< -ostream &operator <<(ostream &os, vector&v) -{ - os<<"[ "; - for(int i=0; i -ostream &operator <<(ostream &os, set&v) -{ - os<<"[ "; - for(T i:v) - { - os< -using namespace std; - -#define LL long long -#define PII pair -#define PLL pair -#define MP make_pair -#define F first -#define S second - -#define ALL(x) (x).begin(), (x).end() -#define DBG(x) cout << __LINE__ << " says: " << #x << " = " << (x) << endl -#define READ freopen("alu.txt", "r", stdin) -#define WRITE freopen("vorta.txt", "w", stdout) - -#include -#include -using namespace __gnu_pbds; - -templateusing indexed_set = tree,rb_tree_tag, tree_order_statistics_node_update>; - -/** - -PBDS -------------------------------------------------- -1) insert(value) -2) erase(value) -3) order_of_key(value) // 0 based indexing -4) *find_by_order(position) // 0 based indexing - -**/ - -template -ostream &operator <<(ostream &os, pair&p); -template -ostream &operator <<(ostream &os, vector&v); -template -ostream &operator <<(ostream &os, set&v); - -inline void optimizeIO() -{ - ios_base::sync_with_stdio(false); - cin.tie(NULL); -} - -const int nmax = 2e5+7; -const LL LINF = 1e17; - -template -string to_str(T x) -{ - stringstream ss; - ss<> &v) -{ - int n = v.size(); - - { - vectorcnt(n); /// for counting sort - - for(auto el:v) - cnt[el.F.S]++; /// first sort according to second element - - vector> nv(n); - vectorpos(n); /// pos[i] = first empty position in bucket i - - pos[0] = 0; - for(int i=1; icnt(n); /// for counting sort - - for(auto el:v) - cnt[el.F.F]++; /// first sort according to second element - - vector> nv(n); - vectorpos(n); /// pos[i] = first empty position in bucket i - - pos[0] = 0; - for(int i=1; i buildSuffixArray(string s) -{ - s += "$"; - - int n = s.size(); - - /// init p , c - - vectorv(n); - - for(int i=0; ip(n), c(n); - for(int i=0; i>v(n); - - for(int i=0; i &ara) -{ - while(lo<=hi) - { - int mid = lo + (hi-lo)/2; - - string chk = s.substr(ara[mid],key.size()); - - if(chkkey) hi = mid - 1; - else return true; - } - - return false; -} - -int lowerBound(int lo,int hi,string s,string key,vector &ara) -{ - while(lo!=hi) - { - int mid = lo + (hi-lo)/2; - - string chk = s.substr(ara[mid],key.size()); - - if(chk &ara) -{ - while(lo!=hi) - { - int mid = lo + (hi-lo)/2; - - string chk = s.substr(ara[mid],key.size()); - - if(chk<=key) lo = mid + 1; - else hi = mid; - } - - return lo; -} - -int main() -{ - optimizeIO(); - - string s; - cin>>s; - - int n = s.size(); - - vectorp = buildSuffixArray(s); - -// for(int i=0; i<(int)p.size(); i++) -// cout<>q; - - while(q--) - { - string sub; - cin>>sub; - - int lo = lowerBound(0,n+1,s,sub,p); - int hi = upperBound(0,n+1,s,sub,p); - - cout< -ostream &operator <<(ostream &os, pair&p) -{ - os<<"{"< -ostream &operator <<(ostream &os, vector&v) -{ - os<<"[ "; - for(int i=0; i -ostream &operator <<(ostream &os, set&v) -{ - os<<"[ "; - for(T i:v) - { - os< -using namespace std; - -#define LL long long -#define PII pair -#define PLL pair -#define MP make_pair -#define F first -#define S second - -#define ALL(x) (x).begin(), (x).end() -#define DBG(x) cout << __LINE__ << " says: " << #x << " = " << (x) << endl -#define READ freopen("alu.txt", "r", stdin) -#define WRITE freopen("vorta.txt", "w", stdout) - -#include -#include -using namespace __gnu_pbds; - -templateusing indexed_set = tree,rb_tree_tag, tree_order_statistics_node_update>; - -/** - -PBDS -------------------------------------------------- -1) insert(value) -2) erase(value) -3) order_of_key(value) // 0 based indexing -4) *find_by_order(position) // 0 based indexing - -**/ - -template -ostream &operator <<(ostream &os, pair&p); -template -ostream &operator <<(ostream &os, vector&v); -template -ostream &operator <<(ostream &os, set&v); - -inline void optimizeIO() -{ - ios_base::sync_with_stdio(false); - cin.tie(NULL); -} - -const int nmax = 2e5+7; -const LL LINF = 1e17; - -template -string to_str(T x) -{ - stringstream ss; - ss< prefix_function(string s) { - int n = (int)s.length(); - vector 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; -} - -int countSubstring(string s,string sub) -{ - string v = sub + "#" + s; - - vectorpre = prefix_function(v); - - int subsz = sub.size(); - - int cnt = 0; - - for(int i=subsz+1;i>tc; - - for(int qq=1;qq<=tc;qq++) - { - string s,sub; - cin>>s>>sub; - - int ans = countSubstring(s,sub); - - cout<<"Case "< -ostream &operator <<(ostream &os, pair&p) -{ - os<<"{"< -ostream &operator <<(ostream &os, vector&v) -{ - os<<"[ "; - for(int i=0; i -ostream &operator <<(ostream &os, set&v) -{ - os<<"[ "; - for(T i:v) - { - os< -using namespace std; - -#define LL long long -#define PII pair -#define PLL pair -#define MP make_pair -#define F first -#define S second - -#define ALL(x) (x).begin(), (x).end() -#define DBG(x) cout << __LINE__ << " says: " << #x << " = " << (x) << endl -#define READ freopen("alu.txt", "r", stdin) -#define WRITE freopen("vorta.txt", "w", stdout) - -#include -#include -using namespace __gnu_pbds; - -templateusing indexed_set = tree,rb_tree_tag, tree_order_statistics_node_update>; - -/** - -PBDS -------------------------------------------------- -1) insert(value) -2) erase(value) -3) order_of_key(value) // 0 based indexing -4) *find_by_order(position) // 0 based indexing - -**/ - -template -ostream &operator <<(ostream &os, pair&p); -template -ostream &operator <<(ostream &os, vector&v); -template -ostream &operator <<(ostream &os, set&v); - -inline void optimizeIO() -{ - ios_base::sync_with_stdio(false); - cin.tie(NULL); -} - -const int nmax = 2e5+7; -const LL LINF = 1e17; - -template -string to_str(T x) -{ - stringstream ss; - ss< &p,vector &c) -{ - int n = p.size(); - - vectorcnt(n); - - for(auto el:c) - cnt[el]++; - - vectornp(n); - vectorpos(n); /// pos[i] = first empty position in bucket i - - pos[0] = 0; - for(int i=1; i buildSuffixArray(string s) -{ - s += "$"; - - int n = s.size(); - - /// init p , c - - vectorv(n); - - for(int i=0; ip(n), c(n); - for(int i=0; inc(n); - - nc[p[0]] = 0; - for(int i=1; i buildLCPArray(string s,vector &p) -{ - s+="$"; - int n = s.size(); - - vectorrnk(n); - - for(int i=0;ilcp(n); - - int k = 0; - - for(int i=0;i>s; - - vectorp = buildSuffixArray(s); - vectorlcp = buildLCPArray(s,p); - - LL repeat = 0; - for(int i=1; i<(int)lcp.size(); i++) - repeat += lcp[i]; - - LL distinct = totalSubstring(s.size()) - repeat; - cout< -ostream &operator <<(ostream &os, pair&p) -{ - os<<"{"< -ostream &operator <<(ostream &os, vector&v) -{ - os<<"[ "; - for(int i=0; i -ostream &operator <<(ostream &os, set&v) -{ - os<<"[ "; - for(T i:v) - { - os< -using namespace std; - -#define LL long long -#define PII pair -#define PLL pair -#define MP make_pair -#define F first -#define S second - -#define ALL(x) (x).begin(), (x).end() -#define DBG(x) cout << __LINE__ << " says: " << #x << " = " << (x) << endl -#define READ freopen("alu.txt", "r", stdin) -#define WRITE freopen("vorta.txt", "w", stdout) - -#include -#include -using namespace __gnu_pbds; - -templateusing indexed_set = tree,rb_tree_tag, tree_order_statistics_node_update>; - -/** - -PBDS -------------------------------------------------- -1) insert(value) -2) erase(value) -3) order_of_key(value) // 0 based indexing -4) *find_by_order(position) // 0 based indexing - -**/ - -template -ostream &operator <<(ostream &os, pair&p); -template -ostream &operator <<(ostream &os, vector&v); -template -ostream &operator <<(ostream &os, set&v); - -inline void optimizeIO() -{ - ios_base::sync_with_stdio(false); - cin.tie(NULL); -} - -const int nmax = 2e5+7; -const LL LINF = 1e17; - -template -string to_str(T x) -{ - stringstream ss; - ss< &p,vector &c) -{ - int n = p.size(); - - vectorcnt(n); - - for(auto el:c) - cnt[el]++; - - vectornp(n); - vectorpos(n); /// pos[i] = first empty position in bucket i - - pos[0] = 0; - for(int i=1; i buildSuffixArray(string s) -{ -// s += "$"; - - int n = s.size(); - - /// init p , c - - vectorv(n); - - for(int i=0; ip(n), c(n); - for(int i=0; inc(n); - - nc[p[0]] = 0; - for(int i=1; i buildLCPArray(string s,vector &p) -{ -// s+="$"; - int n = s.size(); - - vectorrnk(n); - - for(int i=0;ilcp(n); - - int k = 0; - - for(int i=0;i>s1>>s2; - - string s = s1 + char(38) + s2 + char(37) ; - - vectorp = buildSuffixArray(s); - vectorlcp = buildLCPArray(s,p); - -// for(int i=0; i<(int)p.size(); i++) -// cout<first_len) || (now>first_len && prevmx) - { - mx = lcp[i]; - ans = s.substr(now,mx); - } - } - } - -// cout< -ostream &operator <<(ostream &os, pair&p) -{ - os<<"{"< -ostream &operator <<(ostream &os, vector&v) -{ - os<<"[ "; - for(int i=0; i -ostream &operator <<(ostream &os, set&v) -{ - os<<"[ "; - for(T i:v) - { - os< -using namespace std; - -#define LL long long -#define PII pair -#define PLL pair -#define MP make_pair -#define F first -#define S second - -#define ALL(x) (x).begin(), (x).end() -#define DBG(x) cout << __LINE__ << " says: " << #x << " = " << (x) << endl -#define READ freopen("alu.txt", "r", stdin) -#define WRITE freopen("vorta.txt", "w", stdout) - -#include -#include -using namespace __gnu_pbds; - -templateusing indexed_set = tree,rb_tree_tag, tree_order_statistics_node_update>; - -/** - -PBDS -------------------------------------------------- -1) insert(value) -2) erase(value) -3) order_of_key(value) // 0 based indexing -4) *find_by_order(position) // 0 based indexing - -**/ - -template -ostream &operator <<(ostream &os, pair&p); -template -ostream &operator <<(ostream &os, vector&v); -template -ostream &operator <<(ostream &os, set&v); - -inline void optimizeIO() -{ - ios_base::sync_with_stdio(false); - cin.tie(NULL); -} - -const int nmax = 2e5+7; -const LL LINF = 1e17; - -template -string to_str(T x) -{ - stringstream ss; - ss< prefix_function(string s) -{ - int n = (int)s.length(); - vector 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; -} - -vector countPrefixOccurence(string s,const vector &pi) -{ - int n = s.size(); - - vector ans(n + 1); /// ans[i] = the number of occurrence of prefix of length i - - for (int i = 0; i < n; i++) - ans[pi[i]]++; /// count the largest prefixes ending at i - for (int i = n-1; i > 0; i--) - ans[pi[i-1]] += ans[i]; /// adding smaller prefixes which also end at i - for (int i = 0; i <= n; i++) - ans[i]++; /// adding the prefix itself to the count - - return ans; -} - -int main() -{ - optimizeIO(); - - while(1) - { - string s; - cin>>s; - - vectorpi = prefix_function(s); - cout<cnt = countPrefixOccurence(s,pi); - cout< -ostream &operator <<(ostream &os, pair&p) -{ - os<<"{"< -ostream &operator <<(ostream &os, vector&v) -{ - os<<"[ "; - for(int i=0; i -ostream &operator <<(ostream &os, set&v) -{ - os<<"[ "; - for(T i:v) - { - os<