Skip to content

Commit dcb3aa8

Browse files
committed
Practise 20-Jul-2020
1 parent 9a18785 commit dcb3aa8

File tree

3 files changed

+212
-0
lines changed

3 files changed

+212
-0
lines changed

backtracking/n_queen_columnwise.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
#define MAX 10
5+
6+
void printBoard(int board[MAX][MAX], int n){
7+
for(int i=0;i<n;i++){
8+
for(int j=0;j<n;j++){
9+
cout<<board[i][j]<<" ";
10+
}
11+
cout<<endl;
12+
}
13+
cout<<endl;
14+
}
15+
16+
bool isSafe(int board[MAX][MAX], int row, int col, int n){
17+
// only check the left portion of board as no queen is placed in right side
18+
// horizontal row check
19+
for(int i=0;i<col;i++)
20+
if(board[row][i]) return false;
21+
// upper left diagonal check
22+
for(int i=row, j=col; i>=0 && j>=0; i--, j--)
23+
if(board[i][j]) return false;
24+
// lower left diagonal check
25+
for(int i=row, j=col; i<n && j>=0; i++, j--)
26+
if(board[i][j]) return false;
27+
28+
return true;
29+
}
30+
31+
bool nQueen(int board[MAX][MAX], int n, int col){
32+
if(col >= n){
33+
printBoard(board, n); // base case
34+
return true;
35+
}
36+
bool res = false;
37+
for(int i=0;i<n;i++){
38+
if(isSafe(board, i, col, n)){
39+
board[i][col]=1;
40+
res= nQueen(board, n, col+1) || res;
41+
// if code reaches here, then backtrack
42+
board[i][col]=0;
43+
}
44+
}
45+
return res;
46+
}
47+
48+
int main(){
49+
int n;
50+
cin>>n;
51+
int board[MAX][MAX];
52+
memset(board, 0, sizeof(board));
53+
54+
if(!nQueen(board, n, 0)) // starting col --> 0 -> we are placing queens column wise
55+
cout<<"No solution exists\n";
56+
57+
return 0;
58+
}

backtracking/n_queen_rowwise.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
#define MAX 10
5+
6+
void printBoard(int board[MAX][MAX], int n){
7+
for(int i=0;i<n;i++){
8+
for(int j=0;j<n;j++){
9+
cout<<board[i][j]<<" ";
10+
}
11+
cout<<endl;
12+
}
13+
cout<<endl;
14+
}
15+
16+
bool isSafe(int board[MAX][MAX], int row, int col, int n){
17+
// only check the upper portion of board as no queen is placed in lower side
18+
// vertical col check
19+
for(int i=0;i<row;i++)
20+
if(board[i][col]) return false;
21+
// upper left diagonal check
22+
for(int i=row, j=col; i>=0 && j>=0; i--, j--)
23+
if(board[i][j]) return false;
24+
// upper right diagonal check
25+
for(int i=row, j=col; i>=0 && j<n; i--, j++)
26+
if(board[i][j]) return false;
27+
28+
return true;
29+
}
30+
31+
bool nQueen(int board[MAX][MAX], int n, int row){
32+
if(row >= n){
33+
printBoard(board, n); // base case
34+
return true;
35+
}
36+
bool res = false;
37+
for(int i=0;i<n;i++){
38+
if(isSafe(board, row, i, n)){
39+
board[row][i]=1;
40+
res= nQueen(board, n, row+1) || res;
41+
// if code reaches here, then backtrack
42+
board[row][i]=0;
43+
}
44+
}
45+
return res;
46+
}
47+
48+
int main(){
49+
int n;
50+
cin>>n;
51+
int board[MAX][MAX];
52+
memset(board, 0, sizeof(board));
53+
54+
if(!nQueen(board, n, 0)) // starting row --> 0 -> we are placing queens column wise
55+
cout<<"No solution exists\n";
56+
57+
return 0;
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
Given a string S, consider all duplicated substrings: (contiguous) substrings of S that occur 2 or more times. (The occurrences may overlap.)
2+
3+
Return any duplicated substring that has the longest possible length. (If S does not have a duplicated substring, the answer is "".)
4+
5+
6+
7+
Example 1:
8+
9+
Input: "banana"
10+
Output: "ana"
11+
Example 2:
12+
13+
Input: "abcd"
14+
Output: ""
15+
16+
17+
Note:
18+
19+
2 <= S.length <= 10^5
20+
S consists of lowercase English letters.
21+
22+
23+
24+
25+
26+
#define p 10000007
27+
//#define lli long long int
28+
29+
vector<int> roll;
30+
31+
class Solution {
32+
bool match(string s,int len,int size,string& ans)
33+
{
34+
unordered_map<int,vector<int>> m; //Key->hashValue...Value->starting index of substring
35+
int hash=0; //curr window hash
36+
for(int i=0;i<size;++i)
37+
hash = (hash*26 + (s[i]-'a'))%p;
38+
39+
m[hash].push_back(0);
40+
//Rolling hash (sliding window)
41+
for(int j=size;j<len;++j)
42+
{
43+
hash = ((hash-roll[size-1]*(s[j-size]-'a'))%p + p)%p;
44+
hash = (hash*26 + (s[j]-'a'))%p;
45+
if(m.find(hash)!=m.end())
46+
{
47+
for(auto start: m[hash])
48+
{
49+
string temp = s.substr(start,size);
50+
string curr = s.substr(j-size+1,size);
51+
if(temp.compare(curr)==0)
52+
{
53+
ans = temp;
54+
return true;
55+
}
56+
}
57+
}
58+
m[hash].push_back(j-size+1);
59+
}
60+
return false;
61+
}
62+
63+
public:
64+
string longestDupSubstring(string S) {
65+
ios_base::sync_with_stdio(false);
66+
cin.tie(NULL);
67+
68+
int len = S.size();
69+
if(len==0)
70+
return "";
71+
72+
//Store all rolling hash values
73+
roll.resize(len); //Allocating fixed space to array
74+
roll[0] = 1; //Since 26^0 = 1
75+
for(int i=1;i<len;++i)
76+
roll[i] = (26*roll[i-1])%p;
77+
78+
int low=1,high=len,mid;
79+
string ans = "",temp;
80+
while(low<=high)
81+
{
82+
temp="";
83+
mid = low+(high-low)/2;
84+
bool flag = match(S,len,mid,temp);
85+
if(flag)
86+
{
87+
if(temp.size()>ans.size())
88+
ans=temp;
89+
low = mid+1;
90+
}
91+
else
92+
high = mid-1;
93+
}
94+
return ans;
95+
}
96+
};

0 commit comments

Comments
 (0)