Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Блог пользователя imrmnabil

Автор imrmnabil, история, 8 месяцев назад, По-английски

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.

  • Проголосовать: нравится
  • +6
  • Проголосовать: не нравится

»
8 месяцев назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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

  • »
    »
    8 месяцев назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    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.

»
8 месяцев назад, # |
  Проголосовать: нравится -9 Проголосовать: не нравится

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

»
8 месяцев назад, # |
  Проголосовать: нравится +18 Проголосовать: не нравится

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.