結果

問題 No.2102 [Cherry Alpha *] Conditional Reflection
ユーザー kakel-san
提出日時 2022-10-15 00:22:37
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 902 ms / 3,000 ms
コード長 2,960 bytes
コンパイル時間 5,322 ms
コンパイル使用メモリ 109,056 KB
実行使用メモリ 215,660 KB
最終ジャッジ日時 2024-06-26 18:26:54
合計ジャッジ時間 30,339 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 70
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static int NN => int.Parse(ReadLine());
    static string[] SList(int n) => Enumerable.Repeat(0, n).Select(_ => ReadLine()).ToArray();
    static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var n = NN;
        var s = SList(n);
        WriteLine(Ref(n, s));
    }
    static string Ref(int n, string[] s)
    {
        var maxlen = s.Max(si => si.Length);
        mul = new long[maxlen];
        mul[0] = 1;
        for (var i = 1; i < mul.Length; ++i) mul[i] = mul[i - 1] * 31 % mod;
        mul2 = new long[maxlen];
        mul2[0] = 2;
        for (var i = 1; i < mul2.Length; ++i) mul2[i] = mul2[i - 1] * 41 % mod2;
        var set = new Dictionary<long, string>[maxlen];
        for (var i = 0; i < set.Length; ++i) set[i] = new Dictionary<long, string>();
        var set2 = new Dictionary<long, string>[maxlen];
        for (var i = 0; i < set2.Length; ++i) set2[i] = new Dictionary<long, string>();
        var res = new bool[n];
        for (var i = 0; i < n; ++i)
        {
            var h = Hash(s[i]);
            var h2 = Hash2(s[i]);
            var flg = false;
            var len = s[i].Length - 1;
            if (set[len].ContainsKey(h) && set2[len].ContainsKey(h2))
            {
                if (set[len][h] == set2[len][h2]) flg = true;
            }
            for (var j = 0; j + 1 < s[i].Length; ++j)
            {
                if (flg) break;
                var sw1 = Swap(s[i], h, j);
                var sw2 = Swap2(s[i], h2, j);
                if (set[len].ContainsKey(sw1) && set2[len].ContainsKey(sw2))
                {
                    if (set[len][sw1] == set2[len][sw2]) flg = true;
                }
            }
            res[i] = flg;
            set[len][h] = s[i];
            set2[len][h2] = s[i];
        }
        return string.Join("\n", res.Select(f => f ? "Yes" : "No"));
    }
    static int mod = 1_000_000_009;
    static long[] mul = new long[0];
    static long Hash(string a)
    {
        var res = 0L;
        for (var i = 0; i < a.Length; ++i) res = (res + (a[i] - 'a') * mul[i]) % mod;
        return res;
    }
    static long Swap(string a, long hash, int pos)
    {
        return (hash + (a[pos + 1] - a[pos] + mod) * mul[pos] % mod + (a[pos] - a[pos + 1] + mod) * mul[pos + 1] % mod) % mod;
    }
    static int mod2 = 1_110_000_019;
    static long[] mul2 = new long[0];
    static long Hash2(string a)
    {
        var res = 0L;
        for (var i = 0; i < a.Length; ++i) res = (res + (a[i] - 'a') * mul2[i]) % mod2;
        return res;
    }
    static long Swap2(string a, long hash, int pos)
    {
        return (hash + (a[pos + 1] - a[pos] + mod2) * mul2[pos] % mod2 + (a[pos] - a[pos + 1] + mod2) * mul2[pos + 1] % mod2) % mod2;
    }
}
0