Skip to content

Commit b7d4a26

Browse files
committed
p5 done
1 parent 6eef4cc commit b7d4a26

File tree

28 files changed

+448633
-0
lines changed

28 files changed

+448633
-0
lines changed

commands.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ for i in {0..2} ; do g++ s.cpp && ./a.out < "../test-cases/input/input0${i}.txt"
77

88
for i in {3..5} ; do g++ m.cpp && ./a.out > "../test-cases/input/input0${i}.txt"; sleep 1; done
99
for i in {6..8} ; do g++ m.cpp && ./a.out > "../test-cases/input/input0${i}.txt"; sleep 1; done
10+
for i in {3..5} ; do g++ s.cpp && ./a.out < "../test-cases/input/input0${i}.txt" > "../test-cases/output/output0${i}.txt" ; done
11+
for i in {6..8} ; do g++ s.cpp && ./a.out < "../test-cases/input/input0${i}.txt" > "../test-cases/output/output0${i}.txt" ; done
1012
for i in {3..8} ; do g++ s.cpp && ./a.out < "../test-cases/input/input0${i}.txt" > "../test-cases/output/output0${i}.txt" ; done
1113

1214
g++ s.cpp && ./a.out < "../test-cases/input/input00.txt" > "../test-cases/output/output00.txt"

problem-3/author-solutions/b.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ def isBalanced(x):
77
leftSum += ord(s[i]) - ord('0')
88
rightSum += ord(s[n -1 -i]) - ord('0')
99
return leftSum == rightSum
10+
1011
n = int(input())
1112
v = [int(x) for x in input().split()]
1213
count = 0

problem-5/author-solutions/.gitkeep

Whitespace-only changes.

problem-5/author-solutions/s.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include<iostream>
2+
using namespace std;
3+
struct Node {
4+
Node* children[26]{};
5+
};
6+
void insert(Node* root, string s) {
7+
for (int i=0; s[i]; i++) {
8+
int idx = s[i] - 'a';
9+
if (!root->children[idx]) {
10+
root->children[idx] = new Node();
11+
}
12+
root = root->children[idx];
13+
}
14+
}
15+
void solve(Node* root, string& s) {
16+
bool leaf = true;
17+
for(int idx=0; idx<26; idx++) {
18+
if (root->children[idx]) {
19+
leaf = false;
20+
s.push_back('a' + idx);
21+
solve(root->children[idx], s);
22+
s.pop_back();
23+
}
24+
}
25+
if (leaf) cout << s << '\n';
26+
}
27+
int main(void) {
28+
int n; cin >> n;
29+
Node* root = new Node();
30+
for (int i=0; i<n; i++) {
31+
string s; cin >> s;
32+
insert(root, s);
33+
}
34+
string s;
35+
solve(root, s);
36+
return 0;
37+
}

problem-5/author-solutions/s.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Solution {
5+
static class Node {
6+
Node[] children;
7+
Node() {
8+
children = new Node[26];
9+
for(int idx=0; idx<26; idx++) {
10+
children[idx] = null;
11+
}
12+
}
13+
}
14+
15+
static void insert(Node root, String s) {
16+
int n = s.length();
17+
for (int i=0; i < n; i++) {
18+
int idx = (int)(s.charAt(i) - 'a');
19+
if (root.children[idx] == null) {
20+
root.children[idx] = new Node();
21+
}
22+
root = root.children[idx];
23+
}
24+
}
25+
static void solve(Node root, StringBuffer s) {
26+
boolean leaf = true;
27+
for(int idx=0; idx<26; idx++) {
28+
if (root.children[idx] != null) {
29+
leaf = false;
30+
s.append((char)('a' + idx));
31+
solve(root.children[idx], s);
32+
s.deleteCharAt(s.length() - 1);
33+
}
34+
}
35+
if (leaf) System.out.println(s);
36+
}
37+
38+
public static void main(String[] args) {
39+
Scanner sc=new Scanner(System.in);
40+
int n=sc.nextInt();
41+
Node root = new Node();
42+
for (int i=0; i<n; i++) {
43+
String s = sc.next();
44+
insert(root, s);
45+
}
46+
StringBuffer s = new StringBuffer();
47+
solve(root, s);
48+
}
49+
}

problem-5/author-solutions/s.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Node:
2+
def __init__(self):
3+
self.children = [None for _ in range(26)]
4+
5+
def insert(root, s):
6+
for i in range(len(s)):
7+
idx = ord(s[i]) - ord('a')
8+
if (root.children[idx] is None):
9+
root.children[idx] = Node()
10+
root = root.children[idx]
11+
12+
def solve(root, s):
13+
leaf = True
14+
for idx in range(26):
15+
if (root.children[idx] is not None):
16+
leaf = False
17+
s.append(chr(ord('a') + idx))
18+
solve(root.children[idx], s)
19+
s.pop()
20+
if (leaf):
21+
print(''.join(s))
22+
23+
n = int(input())
24+
root = Node()
25+
for i in range(n):
26+
s = input()
27+
insert(root, s)
28+
29+
s = []
30+
solve(root, s)

problem-5/statement.tex

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
**Challenge Name**
2+
Experiment on Word Properties
3+
4+
**Description**
5+
Remove words whose properties are already present in some other words
6+
7+
**Problem Statement**
8+
The Vice Chair of [CS-GPDC](https://www.linkedin.com/company/cs-gpdc), [Sachin](https://www.linkedin.com/in/sachin-sharma-cu/), is running an experiment to study the properties of words. A word is a sequence of letters, $W = L_1 ... L_N$ of length $N$, and has $N$ properties. Through his previous experiments, Sachin has already established that when a new letter $L$ is added to a word $W_0$ of length $N$ to form a new word $W_1 = concat(W_0, L)$ of length $(N+1)$, it retains the original $N$ properties of the original word $W_0$, and gains a new $(N+1)^{th}$ unique property generated by the concatenation of $W_0$ and $L$. Sachin wants to analyze all the properties of words in a given list. However, since the properties of some of the words are already present in some other words, he wants you to filter out such words from the given list and generate the final list of words in ascending order.
9+
10+
**Sample Example**
11+
12+
```text
13+
5
14+
app
15+
banana
16+
a
17+
apple
18+
abc
19+
```
20+
21+
The properties of the word **app** are already present in **apple**; hence, we filter it out.
22+
The properties of the word **banana** are not present in any of the words in the list; hence, we keep it.
23+
The properties of the word **a** are already present in **app** and **apple**; hence, we filter it out.
24+
The properties of the word **apple** are not present in any of the words in the list; hence, we keep it.
25+
The properties of the word **abc** are not present in any of the words in the list; hence, we keep it.
26+
27+
Hence, we print each word on a new line from the final list of words {**banana**, **apple**, **abc**} in ascending order {**abc**, **apple**, **banana**}.
28+
29+
**Input Format**
30+
The first line contains an integer $N$, the number of words.
31+
The next $N$ lines contain one word $W$ on each line.
32+
33+
**Constraints**
34+
$1 \leq N \leq 10^5$
35+
$1 \leq W.length \leq 10^5$
36+
$1 \leq \sum W.length \leq 2 \times 10^7$
37+
$W$ consists of lowercase English letters only
38+
39+
**Output Format**
40+
Print each word on a new line from the final list of words in ascending order

problem-5/test-case-generator/.gitkeep

Whitespace-only changes.

problem-5/test-case-generator/m.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include<iostream>
2+
#include<unordered_set>
3+
#include<vector>
4+
#include<cmath>
5+
#include<algorithm>
6+
using namespace std;
7+
typedef long long ll;
8+
float getRandFloat() {
9+
return static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
10+
}
11+
ll getRand(ll min, ll max) {
12+
return min + static_cast <ll>( getRandFloat() * ( max - min + 1 ) );
13+
}
14+
string getRand(int n) {
15+
string s;
16+
for (int i=0; i<n; i++) s.push_back('a' + getRand(0, 25));
17+
return s;
18+
}
19+
int main(void) {
20+
srand(time(NULL));
21+
ll n = 300; // 300, 1e5
22+
cout << n <<'\n';
23+
ll first = sqrt(n);
24+
vector<string> v;
25+
unordered_set<string> s;
26+
while (s.size() < first) {
27+
int sn = getRand(1, first);
28+
string t = getRand(sn);
29+
if (s.count(t)) continue;
30+
s.insert(t);
31+
v.push_back(t);
32+
}
33+
while (s.size() < n) {
34+
int si = getRand(0, v.size()-1);
35+
int ci = getRand(1, first);
36+
string t = v[si] + (char)('a' + getRand(0, 25));
37+
if (s.count(t)) continue;
38+
s.insert(t);
39+
v.push_back(t);
40+
}
41+
random_shuffle(v.begin(), v.end());
42+
for(int i=0; i<n; i++) cout << v[i] << '\n';
43+
return 0;
44+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
5
2+
app
3+
banana
4+
a
5+
apple
6+
abc
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
4
2+
cat
3+
car
4+
do
5+
dog
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
4
2+
you
3+
are
4+
almost
5+
there

0 commit comments

Comments
 (0)