C++競プロ学習日記(仮)

( 学習記録であり解説Blogではないです )

Round #89 A. String Task|Codeforces

Problem - A - Codeforcesを解きました。

#include <bits/stdc++.h>
using namespace std;
string f( string S )
{
	string res;
	for( auto&& x : S )
	{
                //★
		if( string( "oayeui" ).find( x ) != string::npos )
		{
			continue;	 
		}
		res += x;
	}
	return res;
}
int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	string S;
	cin >> S;
	
	transform( S.begin(), S.end(), S.begin(), ::tolower );
	
	S = f( S );
	
	for( const auto& x : S )
	{
		cout << "." << x;
	}
	cout << endl;
	return 0;
}

実は★の部分...初めにACしたコードでは↓
if( S[i] == 'o' || S[i] == 'a' || S[i] == 'y' || S[i] == 'e' || S[i] == 'u' || S[i] == 'i' )
としていました...。
それを if( string( "oayeui" ).find( x ) != string::npos ) と書けるの学びでした。
文字列の問題、どうも一文字ずつ判定する時、REPでS[ i ]とかしがちですね...。

#include <bits/stdc++.h>
using namespace std;
string f( string S )
{
	string res;
	for( auto&& x : S )
	{
                //★C++14より
		if( "oayeui"s.find( x ) != string::npos )
		{
			continue;	 
		}
		res += x;
	}
	return res;
}
int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	string S;
	cin >> S;
	
	transform( S.begin(), S.end(), S.begin(), ::tolower );
	
	S = f( S );
	
	for( const auto& x : S )
	{
		cout << "." << x;
	}
	cout << endl;
	return 0;
}

更に★の箇所ですが、C++14だと
string( "oayeui" ) としていた所が "oayeui"s と書ける様です。
s付加するだけー!!便利では!!!(学びその②
(でもCodeforcesはまだC++14未対応なので提出は出来ないです)

( 参考:basic_string::sリテラル - cpprefjp C++日本語リファレンス )