Atcoder beginer contest 236 ABC code style format #ifdef#else#endif ios::sync_with_stdio tie XOR map container vector container


Eight minutes a, the first two, and then the third, the head card fell off. (record the tragic experience.
Just keep grinding the problem.

I found this when thinking about the solution:

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif

Learn the form:

#ifdef identifier
 Segment 1
#else
 Segment 2
#endif

It means: when the identifier has been defined (generally defined with #define command), compile program segment 1, otherwise compile program segment 2.

Understand it briefly

Continue to optimize c + + Code:

ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);

About this thing.
sync_with_stdio: whether it is compatible with the switch of stdio. In c + +, either printf or cout can be used, or the two output modes can be mixed.
Turn off the input / output cache of iostream to make the efficiency of cin and cout almost the same as that of scanf and printf.

Let's look at the first question:
Enter a string, exchange the characters at the A and b positions, and then output.
The code is as follows:

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  cin >> s;
  int a, b;
  cin >> a >> b;
  swap(s[a - 1], s[b - 1]);
  cout << s << '\n';
  return 0;
}

A little bit of code style, header file, using namespace std;, Separate from the problem solving code line feed.
It's worth learning. Use pure c + + Plus ios::sync_with_stdio(false);cin.tie(0); To improve input and output efficiency.
To exchange characters, use the function swap(a,b)

Input n, there are four numbers from 1 to N, of which one number has three, and output this number.
My first idea is to count the digital output of cnt=3. Of course, it needs to be traversed twice.

Consider the mathematical method. The even XOR result of a number is 0. Isn't it? In the end, there are only three numbers left? It saves time and space. That is great!

code:

/**
 *    author:  tourist
 *    created: 23.01.2022 15:00:42       
**/
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int x = 0;
  for (int i = 0; i < 4 * n - 1; i++) {
    int y;
    cin >> y;
    x ^= y;
  }
  cout << x << '\n';
  return 0;
}

What is worth learning is the mathematical formula x^=y, which is skillfully equivalent to an open array and a layer of traversal.

The third question is the question of losing your head. The most efficient is to use vector map, or use it unskilled. Continue to consolidate.

The third question is to input n strings first and then m strings. If there is a string in N in m, output Yes, otherwise output No.

code:

/**
 *    author:  tourist
 *    created: 23.01.2022 15:01:51       
**/
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<bool> res(n, false);
  map<string, int> mp;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    mp[s] = i;
  }
  for (int j = 0; j < m; j++) {
    string s;
    cin >> s;
    res[mp[s]] = true;
  }
  for (int i = 0; i < n; i++) {
    cout << (res[i] ? "Yes" : "No") << '\n';
  }
  return 0;
}

First of all, I forgot to define the most common method of vector???

//Define a vector with 10 integer elements, and the given initial value of each element is 1
vector<bool> res(n,0);
//It means to define the vector bool type res size as n, where the initial value of the element is 0

More than that, the very important map in c++stl!

Start learning map container (expose yourself to be lazy and lazy):
map is an associated container in stl, which provides one-to-one hash mapping. (key and value)
The underlying structure of map is red black tree, which automatically sorts the data, so the elements in map are automatically ordered.
map header file #include < map > of course, universal header is also OK.
Then define: STD:: Map < string, int > person;
The first type is the type of keyword and the second is the type of storage object. This topic defines the keyword of string type as the index and has the associated pointer to int.

There is another clever thing about this code,
string s;
cin >> s;
mp[s] = i;
Use string as index and i as data storage. Great idea.
The next idea is to input the string of m, use the res tag for the existing string, and finally output it.
There is also something worth learning, the last three eye operator
Output cout < < (RES [i]? "Yes": "No");
To use the binocular operator to turn an if else judgment statement into a sentence, the code is more concise.

Keywords: C C++ iOS Algorithm Container

Added by elie on Tue, 25 Jan 2022 22:07:13 +0200