LoginSignup
2
0

More than 3 years have passed since last update.

AtCoder Beginner Contest 137 C(アナグラム判定問題)

Last updated at Posted at 2019-08-11

問題文

文字列aに含まれる文字を何らかの順序で並べることで得られる文字列をaのアナグラムと呼びます。
例えば、”greenbin”は"beginner"のアナグラムです。このように、同じ文字が複数回現れる時はその文字をちょうどその回数だけ使わなければなりません。

N個の文字列s1,s2,...,sNが与えられます。それぞれの文字列は長さが10で英小文字からなり、またこれらの文字列は全て異なります。アナグラムであるようなものの個数を求めてください。

入力例1

3
acornistnt
peanutbomb
constraint

出力例1

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

int main(){
    ll n; cin >> n;
    map<string,ll> m;
    //mapとは、連想配列を扱うためのライブラリの一つのこと。
    //map<キー型, 値型> 変数名;

    for(ll i=0;i<n;i++){
        string s;
        cin >> s;
        sort(s.begin(),s.end());
        m[s]++;
    }

    ll ans=0;
    for(auto p:m){
        ll x=p.second;
        ans+=x*(x-1)/2;
    }
    //for(auto p:m)は、変数mの配列の中からアルファベットが小さい順に参照している。
    //map<キー型, 値型> 変数名;
    //p.firstでキーを、p.secondで値を参照できる。

    cout << ans << endl;
    return 0;
}

auto型とは、コンパイル時に自動で型を予測し、適切な型に変換してくれる優れもの!
for文の書き方はfor(auto i : a) cout << i << endl;のようにも書ける!

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

int main(){

    int a[3] = {1, 2, 3};
    for(int i = 0; i < 3; ++i) cout << a[i] << endl;
    for(auto i : a) cout << i << endl;
}
//出力結果
1
2
3
1
2
3

まとめ

mapとは、連想配列を扱うためのライブラリの一つのこと。
連想配列とは、好きな名前を添字にできる配列のこと (0,1,2を添え字にしなくてもいい)
map<キー型, 値型> 変数名;

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0