Official

A - "atcoder".substr() Editorial by en_translator


For those who are new to learning programming / AtCoder

If you are new to learning programming and do not know where to start, please try Problem A “Welcome to AtCoder” from practice contest. There you can find a sample code for each language.

Also, if you are not familiar with problems in programming contests, we recommend you to try some problems in “AtCoder Beginners Selection” (https://atcoder.jp/contests/abs).

「C++入門 AtCoder Programming Guide for beginners (APG4b)」(https://atcoder.jp/contests/APG4b) is a C++ tutorial for competitive programmers. Sadly, this is only in Japanese too. Also, for those who have started to get used to programming contest, the following materials are available anytime:

「競プロ典型 90 問」(Typical 90 Problems of Competitive Programming) (https://atcoder.jp/contests/typical90) is a collection of typical 90 competitive programming problems; unfortunately, currently the problem statements are all Japanese.

In AtCoder Library Practice Contest, you can practice how to use AtCoder Library, a library of advanced algorithms etc., which is available in AtCoder.


Solution 1. Use 7 if statements

For example, if \(L \le 4 \le R\), you need to output o, the \(4\)-th character of atcoder.
Likewise, you can get accepted for the problem using \(7\) if statements.

Sample code (C++):

#include<bits/stdc++.h>

using namespace std;

int main(){
  int l,r;
  cin >> l >> r;
  if(l<=1 && 1<=r){cout << 'a';}
  if(l<=2 && 2<=r){cout << 't';}
  if(l<=3 && 3<=r){cout << 'c';}
  if(l<=4 && 4<=r){cout << 'o';}
  if(l<=5 && 5<=r){cout << 'd';}
  if(l<=6 && 6<=r){cout << 'e';}
  if(l<=7 && 7<=r){cout << 'r';}
  return 0;
}

Solution 2. Introduce a loop to Solution 1

Consider writing the \(7\) if statements at once with a loop.
Let \(S=\)atcoder, and output the \(i\)-th character of \(S\) if \(L \le i \le R\). Do this in ascending order of \(i\).
Depending on programming language, the initial character of a string may be the \(0\)-th or the \(1\)-th character. For example, the first character of the string type in C++ is the \(0\)-th character.

Sample code (C++):

#include<bits/stdc++.h>

using namespace std;

int main(){
  int l,r;
  cin >> l >> r;
  string s="atcoder";
  for(int i=1;i<=7;i++){
    if(l<=i && i<=r){cout << s[i-1];}
  }
  cout << "\n";
  return 0;
}

Solution 3. Use a loop to enumerate the \(L\)-th through \(R\)-th characters

We can output as follows with a loop that enumerates the \(L\)-th through \(R\)-th characters.
Sample code (C++):

#include<bits/stdc++.h>

using namespace std;

int main(){
  int l,r;
  cin >> l >> r;
  string s="atcoder";
  for(int i=l;i<=r;i++){
    cout << s[i-1];
  }
  cout << "\n";
  return 0;
}

Solution 4. Use a function that handles strings, such as substr

The string type in C++ has a function called substr.
You can extract the \(y\) characters starting from the \(x\)-th character by writing (a variable of type string).substr(\(x\),\(y\)).
Again, note that the initial character of a string is the \(0\)-th character.

Sample code 1(C++):

#include<bits/stdc++.h>

using namespace std;

int main(){
  int l,r;
  cin >> l >> r;
  string s="atcoder";
  cout << s.substr(l-1,r-l+1) << "\n";
  return 0;
}

Sample code 2(C++):

#include<bits/stdc++.h>

using namespace std;

int main(){
  int l,r;
  cin >> l >> r;
  cout << ((string)"atcoder").substr(l-1,r-l+1) << "\n";
  return 0;
}

posted:
last update: