imrmnabil's blog

By imrmnabil, history, 10 months ago, In English

Problem:1922A

The code with char array:

#include <iostream>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
       int n;
       int flag = 0;
       cin>>n;
       char a[n];
       char b[n];
       char c[n];
       cin>>a;
       cin>>b;
       cin>>c;

        for (int i = 0; i < n; ++i) {
            if(a[i] != c[i] && b[i] != c[i]) {
                flag = 1;
                break;
            }
        }
        if(flag)cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
}

Error: wrong answer expected NO, found YES [503rd token]

The code with string:

#include <iostream>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
       int n;
       int flag = 0;
       cin>>n;
       string a;
       string b;
       string c;
       cin>>a;
       cin>>b;
       cin>>c;

        for (int i = 0; i < n; ++i) {
            if(a[i] != c[i] && b[i] != c[i]) {
                flag = 1;
                break;
            }
        }
        if(flag)cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
}

Result: Accepted

The compiler I selected was C++17 for both of the code.

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

»
10 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

I thought you should input char array as follows : char a[n]; for(int i=0;i<n;i++)cin>>a[i]; but now found new way to input char arrays :) Thanks XD

  • »
    »
    10 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I have found the problem. I should have use a[n+1], b[n+1], c[n+1]. Using 'cin' is not the problem here in my case. Thank you.

»
10 months ago, # |
  Vote: I like it -9 Vote: I do not like it

The ostream operator is not defined for the char array by default, you are free to define yours if you want...

»
10 months ago, # |
  Vote: I like it +18 Vote: I do not like it

char[] array is null-terminated, meaning that it must have place for \0 character. If you have $$$n$$$ letters, array length must be at least $$$n + 1$$$ then.

  • »
    »
    10 months ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Thank you very much. This was a silly and serious mistake.