結果

問題 No.2419 MMA文字列2
ユーザー dmasupro
提出日時 2023-08-14 21:22:01
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,029 bytes
コンパイル時間 943 ms
コンパイル使用メモリ 93,356 KB
最終ジャッジ日時 2025-02-16 08:09:19
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 13 WA * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>

int main() {
    std::string S;
    std::cin >> S;
    
    std::unordered_set<char> alpha_set(S.begin(), S.end());
    
    // ruiseki[i][s]をS[:i]のアルファベットsの出現回数とする
    std::vector<std::unordered_map<char, int>> ruiseki(S.length() + 1);
    
    for (int i = 0; i < S.length(); ++i) {
        char A = S[i];
        int idx = i + 1;
        
        for (char MM : alpha_set) {
            ruiseki[idx][MM] = ruiseki[idx - 1][MM];
        }
        ruiseki[idx][A] += 1;
    }
    
    int ans = 0;
    for (int i = 0; i < S.length(); ++i) {
        char A = S[i];
        
        for (const auto& entry : ruiseki[i]) {
            char MM = entry.first;
            
            if (A == MM) {
                continue;
            }
            
            ans += ruiseki[i][MM] * (ruiseki[i][MM] - 1) / 2;
        }
    }
    
    std::cout << ans << std::endl;
    
    return 0;
}
0