Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimized code #282

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 24 additions & 35 deletions WordBreak.cpp
Original file line number Diff line number Diff line change
@@ -1,53 +1,42 @@
// Word Break Medium
// Given a string A and a dictionary of n words B, find out if A can be segmented into a space-separated sequence of dictionary words.
// Note: From the dictionary B each word can be taken any number of times and in any order.


#include <bits/stdc++.h>
using namespace std;

class Solution
{
class Solution {
public:
int wordBreak(string A, vector<string> &B) {
unordered_map<string, int> dypr;
unordered_set<string> dict(B.begin(), B.end()); // Convert vector to set for O(1) lookups
int len = A.length();
if(len == 0) return 1;
if(dypr[A] !=0) return dypr[A];

for(int i=1; i<=len;i++){
int flag =0;
string str = A.substr(0,i);
for(int j=0; j<B.size();j++){

if(str.compare(B[j]) == 0){
flag =1; break;
vector<bool> dp(len + 1, false);
dp[0] = true; // Base case: empty string can always be segmented

for (int i = 1; i <= len; i++) {
for (int j = 0; j < i; j++) {
// If substring A[j:i] is in the dictionary and A[0:j] can be segmented
if (dp[j] && dict.find(A.substr(j, i - j)) != dict.end()) {
dp[i] = true;
break; // No need to check further
}
}
if(flag == 1 and wordBreak(A.substr(i, len-i), B) == 1) return dypr[A]=1;
}
return dypr[A]= 0;

}

return dp[len] ? 1 : 0; // Return 1 if the entire string can be segmented, otherwise 0
}
};



int main(){
int main() {
int t;
cin>>t;
while(t--){
cin >> t;
while (t--) {
int n;
cin>>n;
vector<string> dict;
for(int i=0;i<n;i++){
string S;
cin>>S;
dict.push_back(S);
cin >> n;
vector<string> dict(n);
for (int i = 0; i < n; i++) {
cin >> dict[i];
}
string line;
cin>>line;
cin >> line;
Solution ob;
cout<<ob.wordBreak(line, dict)<<"\n";
cout << ob.wordBreak(line, dict) << "\n";
}
return 0;
}
177 changes: 46 additions & 131 deletions trie.cpp
Original file line number Diff line number Diff line change
@@ -1,147 +1,62 @@
#include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

#define ar array
#define ll long long

const int MAX_N = 1e5 + 1;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
long long arr[100];
void solve()
{
long long n, temp, remain, k, x, ans = 0;
void solve() {
long long n, x;
cin >> n >> x;
ll exp = log2(n);
bool flag = false;
if (exp & 1 && pow(2, exp) == n)
{
flag = true;
}
if (x == 0 && n % 2 == 1)

// Check if n is a power of 2
if (x == 0 && n % 2 == 1) {
cout << -1 << "\n";
else if (n <= x || flag)
cout << 1 << "\n";
else
{
return;
}


long long counteven = 0, countodd = 0;
int count = 0;
long long ntemp = n;
string s;
while (n > 0)
{
if (n % 2 == 1)
s += '1';
else
s += '0';
n = n / 2;
}
if (n <= x || (n & (n - 1)) == 0) {
// If n is a power of 2 or x is larger than or equal to n
cout << 1 << "\n";
return;
}

n = s.length();
long long sum = 0;
count = 0;
for (int i = 0; i < n; i++)
{
if (s[i] == '1' && i % 2 == 1)
{
arr[count] = pow(2, i);
count++;
}
else if (s[i] == '1' && i == 0)
{
arr[count] = 1;
count++;
}
else if (s[i] == '1' && i % 2 == 0)
{
arr[count] = pow(2, i - 1);
count++;
arr[count] = pow(2, i - 1);
count++;
}
}
int k = 0;
for (int i = 0; i < count; i++)
{
sum += arr[i];
k++;
if (sum <= x)
continue;
else
{
k--;
break;
vector<long long> powers;
for (int i = 0; (1LL << i) <= n; ++i) {
if (n & (1LL << i)) { // Check if the i-th bit is set
if (i == 0) {
powers.push_back(1);
} else if (i % 2 == 0) {
powers.push_back(1LL << (i - 1));
powers.push_back(1LL << (i - 1)); // Add twice for even positions
} else {
powers.push_back(1LL << i);
}
}
if (k == 0)
cout << count - k << "\n";
else
cout << count - k + 1 << "\n";
}

// while (x--)
// {
// temp = n - x;
// while (temp > 0)
// {
// if (count % 2 == 1)
// counteven++;
// else
// countodd++;
// count++;
// temp = temp / 2;
// }
// count = 2 * counteven + countodd;
// ans = min(ans, count);
// counteven = 0;
// countodd = 0;
// }
// cout << ans << "\n";
// k = int(log2(n));
// if (k % 2 == 0 && k != 0)
// k--;
// if (exp & 1 && pow(2, exp) == n - x)
// {
// flag = true;
// }
// if (flag)
// {
// n = 0;
// ans += 2;
// }
// temp = pow(2, k);
// ans += n / temp;
// remain = n % temp;
// n = remain;
// k = int(log2(remain));
// if (k % 2 == 0 && k != 0)
// k--;
// temp = pow(2, k);
// while (remain < x)
// {
// remain = remain - temp;
// ans++;
// }
// ans++;
// if (remain == 0)
// cout << ans << "\n";
// else
// cout << -1 << "\n";
long long sum = 0;
int k = 0;
for (const auto& p : powers) {
sum += p;
k++;
if (sum > x) {
k--; // Decrease count if we exceed x
break;
}
}

cout << (k > 0 ? powers.size() - k + 1 : powers.size() - k) << "\n";
}

int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tc = 1;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);

int tc;
cin >> tc;
for (int t = 1; t <= tc; t++)
{
// cout << "Case #" << t << ": ";
while (tc--) {
solve();
}
}

return 0;
}