Aint_Stain's blog

By Aint_Stain, history, 9 years ago, In English

This code is showing MLE. Why?!!!!!!

1.#include<bits/stdc++.h>

using namespace std;

int main(void) { string a,b,c;

scanf("%s %s %s",&a,&b,&c);
a=a+b;
sort(a.begin(),a.end());
sort(c.begin(),c.end());
if(a==c)
    printf("YES");
else
    printf("NO");

return 0;

}

but when I using this code below it was accepted.What is the fact here between scanf and cin or else?

2.#include<bits/stdc++.h>

using namespace std;

int main(void) { string a,b,c;

cin>>a>>b>>c;
a+=b;
sort(a.begin(),a.end());
sort(c.begin(),c.end());
if(a==c)
    cout<<"YES";
else
    cout<<"NO";

return 0;

}

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

| Write comment?
»
9 years ago, # |
Rev. 2   Vote: I like it +5 Vote: I do not like it

The format specifier you are using requires character array parameter but not a std::string.

In another words, if you want to read using scanf("%s"), you shall declare char a[MAXN], and scan into it.

Read a little bit more about I/O in C++, and differences between cin and scanf.

»
8 years ago, # |
  Vote: I like it 0 Vote: I do not like it

You can't use scanf and take a string(C++ datatype). You can do it for a character array (C string with a null included). C++ string and C string are different.