sjp1114's blog

By sjp1114, history, 8 months ago, In English

Hello. I am trying to solve the problem 266A. I tested my code and it works fine locally. However, the judge is giving wrong answer. I don't know what is the cause of this. Also, when I outputted the value of R after breaking inner while loop, it's somehow 36 when it should theoretically be strictly less than value of n(for one of test case "RRG" that's length 3). I wasn't facing this issue when I ran code locally. What can be the problem? Here it is:

#include <iostream>
using namespace std; 
 
void solve(){
    int n;
    cin >> n; 
    cout << "Value of n: " << n << '\n';
    string s; 
    cin >> s; 
    int R, L = 0; 
    int ans = 0; 
    while(L < n - 1){
        //encountered contiguous block of adjacent stones of same color => extend window far as possible of mathcing color! 
        if(s[L] == s[L+1]){
            cout << "Found contiguous block!" << '\n'; 
            while(R < n && s[R] == s[L]){
                R++; 
            }
            cout << "Cur Left Boundary: " << L << '\n'; 
            cout << "End Right Boundary: " << R << '\n'; 
            //R - L is number of stones adjacent of same color=> need to remove 1 less than it to satisfy requirement of problem => 
            //no two adjacent stones same color! 
            ans += R - L - 1; 
            cout << "Value of ans after adding: " << ans << '\n';
            L = R; 
        } else{
            L++;
            R++;
        }
    }
    cout << ans; 
}
int main(){
    solve(); 
}

Thanks! Much help appreciated.

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By sjp1114, history, 2 years ago, In English

Hello! I am having trouble debugging my code because I don't know why it doesn't work. The logic seems to be straightforward and I feel like I handle edge cases where 0 wraps to 9 and viceversa!

include <bits/stdc++.h>

using namespace std;

void solve() { //number of wheels for the test case we're on! int n; cin >> n; vector wheels(n); //store the initial state of the wheels in some vector! for(int i = 0; i < n; i++){ int cur; cin >> cur; wheels.push_back(cur); } //then iterate through each of n wheels the number and type of moves made! //decode each move! int num_moves; string series; //decode for every ath wheel! for(int a = 0; a<n; a++){ cin >> num_moves >> series; int index = 0; //as long as we didn't process each and every move, keep processing each move to //decypher for current ath wheel! while(num_moves--){ if(series[index] == 'U'){ if(wheels[a] == 0){ wheels[a] = 9; index++; continue; } else{ wheels[a] = wheels[a] — 1; index++; continue; } } else{ if(wheels[a] == 9){ wheels[a] = 0; index++; continue; } else{ wheels[a] = wheels[a] + 1; index++; } } } }

//once we decode for all n wheels, we need to simply print out!
for(int c = 0; c < n; c++){
    cout << wheels[c] << " ";
}
//also start a new line!
cout << endl;

}

int main(){ int t; cin >> t; while(t--){ solve(); }

}

Full text and comments »

  • Vote: I like it
  • -10
  • Vote: I do not like it

By sjp1114, history, 2 years ago, In English

include <bits/stdc++.h>

using namespace std;

void solve(){ string word; cin >> word; unordered_set s; int count = 0;

for(int i = 0; i < word.size() and word[i] != '\0'; i++){
    //check if x is already not in set s!
    if(s.find(word[i]) !=  s.end()){
        s.insert(word[i]);
        count++;
    }
    //otherwise, it's a character we already saw before!
}

//check for parity!
if(count % 2 == 0){
    cout << "CHAT WITH HER!";
    return;
}

else{
    cout << "IGNORE HIM!";
    return;
}

}

int main(){ solve(); }

Full text and comments »

  • Vote: I like it
  • -14
  • Vote: I do not like it

By sjp1114, history, 2 years ago, In English

include <bits/stdc++.h>

using namespace std;

void solve(){ int number; cin >> number; string n = to_string(number);

//run a for loop and count number of lucky digits: either 4 or 7!
int lucky_num = 0;
for(int i = 0; i < n.size() and n[i] != '\0'; i++){
    if(n[i] == '4' or n[i] == '7'){
        lucky_num++;
    }
}
string lucky_num_s = to_string(lucky_num);

if(lucky_num_s.size() == 1){
    if(lucky_num_s[0] == '4'){
        cout << "YES";
        return;
    }
    else if(lucky_num_s[0] == '7'){
        cout << "YES";
        return;
    }
    else{
        cout << "NO";
        return;
    }
}
//boolean flag to check if current num is nearly lucky!
bool is_lucky = true;
for(int j = 0; j < lucky_num_s.size() and lucky_num_s[j] != '\0'; j++){
    if(lucky_num_s[j] == '4'){
        continue;
    }
    else if(lucky_num_s[j] == '7'){
        continue;
    }
    else{
        is_lucky = false;
        break;
    }
}
if(is_lucky){
    cout << "YES";
    return;
}
else{
    cout << "NO";
}

} int main(){ solve(); }

Full text and comments »

  • Vote: I like it
  • -8
  • Vote: I do not like it

By sjp1114, history, 2 years ago, In English

include <bits/stdc++.h>

using namespace std;

int number; int l1,r1,l2,r2;

int main(){ cin >> number; for(int i = 0; i < number; i++){ cin >> l1 >> r1 >> l2 >> r2; //basically, check if the minimum number of min. or max. required //falls in range of minimums and maxiums that must appear for //valid beautiful array! if(l2<=l1<=r2 or l1<=l2<=r1){ cout << max(l1, l2) << endl; continue; } else{ cout << l1 + l2 << endl; } } }

Basically, the input first line contains number of test cases. And for the following lines, each line is a test case containing each 4 integers, each separated by white space character. I was wondering what would be a good way to read from this for each test case. Thanks!

Full text and comments »

  • Vote: I like it
  • +1
  • Vote: I do not like it