forked from Ayon-SSP/The-SDE-Prep
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Ayon-SSP#142 from Dhruv-2134/Word-Break-Problem
Word-Break-Problem fixes Ayon-SSP#114
- Loading branch information
Showing
7 changed files
with
222 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
{ | ||
"files.associations": { | ||
"array": "cpp", | ||
"atomic": "cpp", | ||
"*.tcc": "cpp", | ||
"bitset": "cpp", | ||
"cctype": "cpp", | ||
"cfenv": "cpp", | ||
"charconv": "cpp", | ||
"chrono": "cpp", | ||
"cinttypes": "cpp", | ||
"clocale": "cpp", | ||
"cmath": "cpp", | ||
"codecvt": "cpp", | ||
"complex": "cpp", | ||
"condition_variable": "cpp", | ||
"csetjmp": "cpp", | ||
"csignal": "cpp", | ||
"cstdarg": "cpp", | ||
"cstddef": "cpp", | ||
"cstdint": "cpp", | ||
"cstdio": "cpp", | ||
"cstdlib": "cpp", | ||
"cstring": "cpp", | ||
"ctime": "cpp", | ||
"cuchar": "cpp", | ||
"cwchar": "cpp", | ||
"cwctype": "cpp", | ||
"deque": "cpp", | ||
"forward_list": "cpp", | ||
"list": "cpp", | ||
"unordered_map": "cpp", | ||
"unordered_set": "cpp", | ||
"vector": "cpp", | ||
"exception": "cpp", | ||
"algorithm": "cpp", | ||
"functional": "cpp", | ||
"iterator": "cpp", | ||
"map": "cpp", | ||
"memory": "cpp", | ||
"memory_resource": "cpp", | ||
"numeric": "cpp", | ||
"optional": "cpp", | ||
"random": "cpp", | ||
"ratio": "cpp", | ||
"regex": "cpp", | ||
"set": "cpp", | ||
"string": "cpp", | ||
"string_view": "cpp", | ||
"system_error": "cpp", | ||
"tuple": "cpp", | ||
"type_traits": "cpp", | ||
"utility": "cpp", | ||
"fstream": "cpp", | ||
"future": "cpp", | ||
"initializer_list": "cpp", | ||
"iomanip": "cpp", | ||
"iosfwd": "cpp", | ||
"iostream": "cpp", | ||
"istream": "cpp", | ||
"limits": "cpp", | ||
"mutex": "cpp", | ||
"new": "cpp", | ||
"ostream": "cpp", | ||
"scoped_allocator": "cpp", | ||
"shared_mutex": "cpp", | ||
"sstream": "cpp", | ||
"stdexcept": "cpp", | ||
"streambuf": "cpp", | ||
"thread": "cpp", | ||
"typeindex": "cpp", | ||
"typeinfo": "cpp", | ||
"valarray": "cpp" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int maximumSubarraySum(vector<int> arr, int size) | ||
{ | ||
int maxSum = INT_MIN; | ||
|
||
for (int i = 0; i <= size - 1; i++) | ||
{ | ||
int currSum = 0; | ||
for (int j = i; j <= size - 1; j++) | ||
{ | ||
currSum += arr[j]; | ||
if (currSum > maxSum) | ||
{ | ||
maxSum = currSum; | ||
} | ||
} | ||
} | ||
|
||
return maxSum; | ||
} | ||
int main() | ||
{ | ||
int size; | ||
cout<<"Enter the size of the vector"<<endl; | ||
cin>>size; | ||
vector<int> v(size); | ||
cout<<"Enter the elements in the vector"<<endl; | ||
for(int i=0;i<size;i++) | ||
{ | ||
cin>>v[i]; | ||
} | ||
cout << maximumSubarraySum(v, size) << endl; | ||
return 0; | ||
} |
Binary file not shown.
68 changes: 68 additions & 0 deletions
68
2]_Algorithms/9]_Dynamic Programming/Word_Break_Problem.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// A Dynamic Programming based program to test | ||
// whether a given string can be segmented into | ||
// space separated words in dictionary | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int dictionaryContains(string word) | ||
{ | ||
string dictionary[] = {"mobile", "samsung", "sam", "sung", "man", | ||
"mango", "icecream", "and", "go", "i", | ||
"like", "ice", "cream"}; | ||
int size = sizeof(dictionary) / sizeof(dictionary[0]); | ||
for (int i = 0; i < size; i++) | ||
if (dictionary[i].compare(word) == 0) | ||
return true; | ||
return false; | ||
} | ||
|
||
bool wordBreak(string s) | ||
{ | ||
int n = s.size(); | ||
if (n == 0) | ||
return true; | ||
vector<bool> dp(n + 1, 0); | ||
vector<int> matched_index; | ||
matched_index.push_back(-1); | ||
|
||
for (int i = 0; i < n; i++) | ||
{ | ||
int msize = matched_index.size(); | ||
int f = 0; | ||
for (int j = msize - 1; j >= 0; j--) | ||
{ | ||
string sb = s.substr(matched_index[j] + 1, | ||
i - matched_index[j]); | ||
|
||
if (dictionaryContains(sb)) | ||
{ | ||
f = 1; | ||
break; | ||
} | ||
} | ||
|
||
if (f == 1) | ||
{ | ||
dp[i] = 1; | ||
matched_index.push_back(i); | ||
} | ||
} | ||
return dp[n - 1]; | ||
} | ||
|
||
|
||
int main() | ||
{ | ||
wordBreak("ilikesamsung") ? cout << "Yes\n" | ||
: cout << "No\n"; | ||
wordBreak("iiiiiiii") ? cout << "Yes\n" | ||
: cout << "No\n"; | ||
wordBreak("") ? cout << "Yes\n" : cout << "No\n"; | ||
wordBreak("ilikelikeimangoiii") ? cout << "Yes\n" | ||
: cout << "No\n"; | ||
wordBreak("samsungandmango") ? cout << "Yes\n" | ||
: cout << "No\n"; | ||
wordBreak("samsungandmangok") ? cout << "Yes\n" | ||
: cout << "No\n"; | ||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int rodCutting(int n, int value[]) | ||
{ | ||
int i, j; | ||
|
||
int result[n + 1]; | ||
|
||
result[0] = 0; | ||
|
||
for (i = 1; i <= n; i++) | ||
{ | ||
result[i] = INT_MIN; | ||
|
||
for (j = 0; j < i; j++) | ||
{ | ||
result[i] = max(result[i], value[j] + result[i - (j + 1)]); | ||
} | ||
} | ||
|
||
return result[n]; | ||
} | ||
|
||
int main() | ||
{ | ||
int n; | ||
cout << "Enter the length of the rod" << endl; | ||
cin >> n; | ||
|
||
int value[n]; | ||
|
||
cout << "Enter the values of pieces of rod of all size" << endl; | ||
|
||
for (int i = 0; i < n; i++) | ||
cin >> value[i]; | ||
|
||
cout << "Maximum obtainable value by cutting up the rod in many pieces are" << endl; | ||
cout << rodCutting(n, value); | ||
|
||
cout << endl; | ||
return 0; | ||
} |
Binary file not shown.