Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b617339

Browse files
wordbreak
1 parent 540e8d1 commit b617339

File tree

6 files changed

+267
-70
lines changed

6 files changed

+267
-70
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"tasks": [
3+
{
4+
"type": "cppbuild",
5+
"label": "C/C++: g++ build active file",
6+
"command": "/usr/bin/g++",
7+
"args": [
8+
"-fdiagnostics-color=always",
9+
"-g",
10+
"${file}",
11+
"-o",
12+
"${fileDirname}/${fileBasenameNoExtension}"
13+
],
14+
"options": {
15+
"cwd": "${fileDirname}"
16+
},
17+
"problemMatcher": [
18+
"$gcc"
19+
],
20+
"group": {
21+
"kind": "build",
22+
"isDefault": true
23+
},
24+
"detail": "Task generated by Debugger."
25+
}
26+
],
27+
"version": "2.0.0"
28+
}

‎DSAPractice/recursion/LockFreeStack.java‎

Lines changed: 75 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
/**
33
*
4+
* https://www.codechef.com/viewsolution/55504593
5+
*
46
2 testcase
57
2 no of threads
68
2(no of elements in the list) 1 2 (the list)
@@ -153,86 +155,89 @@ public static void main(String args[]) throws IOException {
153155
br = new BufferedReader(new InputStreamReader(System.in));
154156
int t = nextInt();
155157
while (t-- > 0) {
156-
input();
158+
input();
157159
}
158160

159161
br.close();
160162
}
161163

162-
public static void input()throws IOException
163-
{
164-
// no of threads
165-
int n = nextInt(), c = 0;
166-
// the huge stack
167-
Stack<Integer> s[] = new Stack[n];
168-
for (int i = 0; i < n; i++) {
169-
// stack inside huge stack
170-
s[i] = new Stack<>();
171-
// length of the values
172-
int len = nextInt();
173-
// array for adding in small stack
174-
int a[] = new int[len];
175-
for (int j = 0; j < len; j++) {
176-
a[j] = nextInt();
177-
}
178-
c += len;
179-
for (int k = len - 1; k >= 0; k--) {
180-
s[i].push(a[k]);
181-
}
182-
}
183-
184-
int arr[] = new int[c];
185-
for (int i = 0; i < c; i++) {
186-
arr[i] = nextInt();
187-
}
188-
int z =c-1;boolean flag=false;
189-
if (n == 1) {
190-
Stack<Integer> st = s[0];
191-
while(!st.isEmpty() || z>=0)
192-
{
193-
if(st.peek()==arr[z])
194-
{st.pop(); flag=true;}
195-
else
196-
{
197-
flag=false;
164+
public static void input() throws IOException {
165+
// no of threads
166+
int n = nextInt(), c = 0;
167+
// the huge stack
168+
Stack<Integer> s[] = new Stack[n];
169+
for (int i = 0; i < n; i++) {
170+
// stack inside huge stack
171+
s[i] = new Stack<>();
172+
// length of the values
173+
int len = nextInt();
174+
// array for adding in small stack
175+
int a[] = new int[len];
176+
for (int j = 0; j < len; j++) {
177+
a[j] = nextInt();
178+
}
179+
c += len;
180+
for (int k = len - 1; k >= 0; k--) {
181+
s[i].push(a[k]);
182+
}
183+
}
184+
185+
int arr[] = new int[c];
186+
for (int i = 0; i < c; i++) {
187+
arr[i] = nextInt();
188+
}
189+
int z = c - 1;
190+
boolean flag = false;
191+
if (n == 1) {
192+
Stack<Integer> st = s[0];
193+
while (!st.isEmpty() || z >= 0) {
194+
if (st.peek() == arr[z]) {
195+
st.pop();
196+
flag = true;
197+
} else {
198+
flag = false;
198199
break;
199200
}
200201
z--;
201-
}
202-
if(flag==true){
203-
System.out.println("Yes");return;}
204-
else{
205-
System.out.println("No");return;
206-
}
207-
208-
// Stack<Integer> a = s[0];
209-
// boolean b = true;
210-
// for (int i = c - 1; i >= 0; i--) {
211-
// if (arr[i] == a.peek()) {
212-
// a.pop();
213-
// } else {
214-
// b = false;
215-
// break;
216-
// }
217-
// }
218-
219-
// if (b) {
220-
// System.out.println("Yes");
221-
// } else {
222-
// System.out.println("No");
223-
// }
224-
// return;
225-
}
226-
227-
if (loc_recur(s, arr, c)) {
228-
System.out.println("Yes");
229-
} else {
230-
System.out.println("No");
231-
}
202+
}
203+
if (flag == true) {
204+
System.out.println("Yes");
205+
return;
206+
} else {
207+
System.out.println("No");
208+
return;
209+
}
210+
211+
// Stack<Integer> a = s[0];
212+
// boolean b = true;
213+
// for (int i = c - 1; i >= 0; i--) {
214+
// if (arr[i] == a.peek()) {
215+
// a.pop();
216+
// } else {
217+
// b = false;
218+
// break;
219+
// }
220+
// }
221+
222+
// if (b) {
223+
// System.out.println("Yes");
224+
// } else {
225+
// System.out.println("No");
226+
// }
227+
// return;
228+
}
229+
230+
if (loc_recur(s, arr, c)) {
231+
System.out.println("Yes");
232+
} else {
233+
System.out.println("No");
234+
}
232235
}
236+
233237
public static boolean loc_recur(Stack<Integer> s[], int[] arr, int n) {
234-
if (n == 0)
235-
{return true;}
238+
if (n == 0) {
239+
return true;
240+
}
236241

237242
int x = arr[n - 1];
238243
for (Stack<Integer> st : s) {
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
2+
//https://www.codingninjas.com/codestudio/problems/983635?topList=striver-sde-sheet-problems&utm_source=striver&utm_medium=website
3+
/*
4+
*
5+
*
6+
* void breakword(string &s,unordered_map<string,bool>& mapp,string &tmp,vector<string>&ans,int ind)
7+
{
8+
if(ind==s.size())
9+
{
10+
ans.push_back(tmp);
11+
return;
12+
}
13+
for(int i=ind;i<s.size();i++)
14+
{
15+
string sub = s.substr(ind,i-ind+1);
16+
if(mapp[sub])
17+
{
18+
tmp = tmp+sub+" ";
19+
breakword(s,mapp,tmp,ans,i+1);
20+
//backtrack
21+
for(int i=0;i<=sub.size();i++)
22+
tmp.pop_back();
23+
}
24+
}
25+
}
26+
27+
vector<string> wordBreak(string &s, vector<string> &dictionary)
28+
{
29+
// Write your code here
30+
unordered_map<string,bool> mapp;
31+
for(string ele : dictionary)
32+
mapp[ele] = true;
33+
34+
string tmp="";
35+
vector<string> ans;
36+
breakword(s,mapp,tmp,ans,0);
37+
return ans;
38+
}
39+
*/
40+
import java.io.*;
41+
import java.util.*;
42+
43+
class WordBreakII {
44+
static BufferedReader br;
45+
static StringTokenizer st;
46+
47+
static int nextInt() throws IOException {
48+
return Integer.parseInt(next());
49+
}
50+
51+
static String next() throws IOException {
52+
while (st == null || !st.hasMoreTokens()) {
53+
st = new StringTokenizer(br.readLine());
54+
}
55+
return st.nextToken();
56+
}
57+
58+
static long nextLong() throws IOException {
59+
return Long.parseLong(next());
60+
}
61+
62+
public static void main(String args[]) throws IOException {
63+
br = new BufferedReader(new InputStreamReader(System.in));
64+
int t = nextInt();
65+
while (t-- > 0) {
66+
input();
67+
}
68+
br.close();
69+
}
70+
71+
public static void input() throws IOException {
72+
int k = nextInt();
73+
ArrayList<String> dictionary = new ArrayList<>();
74+
ArrayList<String> ans = new ArrayList<>();
75+
for (int i = 0; i < k; i++) {
76+
dictionary.add(next());
77+
}
78+
String seq = next();
79+
ArrayList<String> sam = new ArrayList<>();
80+
wordBreak(0, sam, ans, seq, dictionary);
81+
82+
System.out.println(ans);
83+
}
84+
85+
static void wordBreak(int ind, ArrayList<String> sam, ArrayList<String> ans,
86+
String s, ArrayList<String> dictionary) {
87+
88+
if (ind == s.length()) {
89+
ans.add(listToString(sam));
90+
// sam.removeAll(sam);
91+
}
92+
93+
// System.out.println("ind value:" + ind);
94+
for (int i = ind; i < s.length(); i++) {
95+
// System.out.println("value of i:" + i);
96+
// System.out.println("value of ind:" + ind);
97+
if (ind == s.length() || i == s.length()) {
98+
break;
99+
}
100+
String firstHalf = s.substring(ind, ind + (i - ind + 1));
101+
// System.out.println("firsthalf:" + firstHalf);
102+
// System.out.println("checking " + firstHalf + " in dictionary...");
103+
if (dictionary.contains(firstHalf)) {
104+
// System.out.println("its present!");
105+
sam.add(firstHalf);
106+
// System.out.println("added to sam...");
107+
// System.out.println("-------------------------------");
108+
// System.out.println(sam);
109+
110+
wordBreak(i + 1, sam, ans, s, dictionary);
111+
// System.out.println("after returning from recursion sam value:" + sam);
112+
sam.remove(sam.size() - 1);
113+
}
114+
// System.out.println("not present! :( ");
115+
116+
}
117+
118+
}
119+
120+
static String listToString(ArrayList<String> h) {
121+
String m = "";
122+
for (int i = 0; i < h.size(); i++) {
123+
m += h.get(i) + " ";
124+
}
125+
return m.trim();
126+
}
127+
128+
}

‎DSAPractice/recursion/stringDic‎

243 KB
Binary file not shown.

‎DSAPractice/recursion/stringDic.cpp‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
void recusrion(int l, string s, set<string> &dic, vector<string> &temp) {
5+
cout<<"ind:"<<l<<endl;
6+
if(l == s.size()) {
7+
for(auto &s : temp)
8+
cout<<s<<" ";
9+
cout<<endl;
10+
}
11+
12+
for(int i = l; i < s.size(); i++) {
13+
string first = s.substr(l, i - l + 1);
14+
cout<<"i:"<<i<<endl;
15+
cout<<"l:"<<l<<endl;
16+
17+
cout<<"first:"<<first<<endl;
18+
if(dic.find(first) != dic.end()) {
19+
cout<<"present in dictionary"<<endl;
20+
temp.push_back(first);
21+
cout<<"temp:"<<first<<endl;
22+
recusrion(i+1, s, dic, temp);
23+
24+
temp.pop_back();
25+
cout<<"temp:"<<first<<endl;
26+
}cout<<"not present in dictionary :("<<endl;
27+
}
28+
29+
}
30+
31+
int main() {
32+
string input = "godisnowherenowhere";
33+
set<string> dic = {"god", "is", "no", "now", "where", "here"};
34+
vector<string> temp;
35+
recusrion(0, input, dic, temp);
36+
}

‎book.pdf‎

1.05 MB
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /