結果

問題 No.1909 Detect from Substrings
ユーザー ruthen71
提出日時 2022-04-22 22:27:51
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 1,646 bytes
コンパイル時間 2,038 ms
コンパイル使用メモリ 208,652 KB
最終ジャッジ日時 2025-01-28 20:27:09
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#ifdef _RUTHEN
#include "debug.hpp"
#else
#define show(...) true
#endif

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> using V = vector<T>;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N, M;
    cin >> N >> M;
    V<string> S(N);
    rep(i, N) cin >> S[i];
    rep(j, M) {
        set<char> st;
        rep(i, N) st.insert(S[i][j]);
        if (st.size() == 1) continue;
        if (st.size() >= 3) {
            cout << 0 << '\n';
            return 0;
        }
        map<char, int> mp;
        rep(i, N) mp[S[i][j]]++;
        show(mp);
        for (auto [c, cnt] : mp) {
            if (N - cnt > 1) {
                st.erase(c);
            }
        }
        show(st);
        int ans = 0;
        for (auto c : st) {
            int ind = -1;
            rep(i, N) {
                if (S[i][j] != c) {
                    // S[i]のj文字目以降に、他のS[i]のj+1文字目以降が含まれているかを確認する
                    ind = i;
                    break;
                }
            }
            int ok = 1;
            show(ind);
            rep(i, N) {
                if (i == ind) continue;
                int k = j + 1;
                for (int sij = j; sij < M; sij++) {
                    if (S[i][k] == S[ind][sij]) k++;
                    if (k == M) break;
                }
                if (k >= M) continue;
                ok = 0;
                break;
            }
            ans += ok;
        }
        cout << ans << '\n';
        return 0;
    }
    return 0;
}
0