Official

A - Spoiler Editorial by en_translator


For beginners

Many solutions are available for this problem.

Solution 1

Split \(S\) into three parts by |, and the answer is the concatenation of the first and third. For example, one can use split method in Python.

Sample code (Python)

S = input()
a, b, c = S.split('|')
print(a+c)

Solution 2

Let \(x\) and \(y\) be the positions of the two occurrences of | in \(S\), then the sought string is the concatenation of the substring prior to the \(x\)-th character and another succeeding the \(y\)-th character. For example, one can use find and substr methods in C++.

Sample code (C++)

#include<bits/stdc++.h>
using namespace std;

int main(){
	string s;
	cin >> s;
	int x = s.find("|");  // The first occurrence of `|` when scanning from the head
	int y = s.rfind("|");  // The first occurrence of `|` when scanning from the tail
	cout << s.substr(0,x) + s.substr(y+1) << endl;
}

Solution 3

Prepare an empty string \(T\). Iterate through the characters of \(S\) from the beginning. Ignore the characters between the first and second occurrences of |, and append the other inspected characters to \(T\). This way, the sought string can be obtained. For example, one can implement it in C++ as follows:

Sample code (C++)

#include<bits/stdc++.h>
using namespace std;

int main(){
	string s;
	cin >> s;
	string t;
	int count = 0;
	for(char c: s){
		if(c= = '|'){
			count++;
		}else{
			if(count != 1) t+=c;
		}
	}
	cout << t << endl;
}

Solution 4

One can use regular expression to obtain the sought string. Note that | must be escaped. For example, one can use re.sub method in Python.

Sample code (Python)

import re
S = input()
print(re.sub("\|.*\|", "", S))

posted:
last update: