結果

問題 No.1994 Confusing Name
ユーザー tnakao0123tnakao0123
提出日時 2022-07-03 00:41:10
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,228 bytes
コンパイル時間 1,908 ms
コンパイル使用メモリ 71,176 KB
実行使用メモリ 20,392 KB
最終ジャッジ日時 2023-08-18 17:37:48
合計ジャッジ時間 6,379 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,840 KB
testcase_01 AC 3 ms
4,820 KB
testcase_02 AC 3 ms
4,820 KB
testcase_03 AC 3 ms
4,808 KB
testcase_04 AC 2 ms
4,788 KB
testcase_05 AC 3 ms
4,976 KB
testcase_06 AC 4 ms
5,028 KB
testcase_07 AC 3 ms
4,916 KB
testcase_08 AC 4 ms
4,992 KB
testcase_09 AC 5 ms
5,116 KB
testcase_10 AC 4 ms
5,024 KB
testcase_11 AC 3 ms
4,976 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 37 ms
7,724 KB
testcase_22 AC 17 ms
6,272 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 56 ms
8,712 KB
testcase_27 WA -
testcase_28 AC 154 ms
13,256 KB
testcase_29 AC 14 ms
6,096 KB
testcase_30 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1994.cc:  No.1994 Confusing Name - yukicoder
 */

#include<cstdio>
#include<string>
#include<map>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 50000;
const int MAX_L = 10;
const int P = 4073;
const int MOD = 1000000009;

/* typedef */

typedef map<int,int> mii;

template<const int MOD>
struct MI {
  int v;
  MI(): v() {}
  MI(int _v): v(_v % MOD) {}
  MI(long long _v): v(_v % MOD) {}

  MI operator+(const MI m) const { return MI(v + m.v); }
  MI operator-(const MI m) const { return MI(v + MOD - m.v); }
  MI operator*(const MI m) const { return MI((long long)v * m.v); }

  MI &operator+=(const MI m) { return (*this = *this + m); }
  MI &operator-=(const MI m) { return (*this = *this - m); }
  MI &operator*=(const MI m) { return (*this = *this * m); }

  MI pow(int n) const {  // a^n % MOD
    MI pm = 1, a = *this;
    while (n > 0) {
      if (n & 1) pm *= a;
      a *= a;
      n >>= 1;
    }
    return pm;
  }

  MI inv() const { return pow(MOD - 2); }
  MI operator/(const MI m) const { return *this * m.inv(); }
  MI &operator/=(const MI m) { return (*this = *this / m); }
};

typedef MI<MOD> mi;

/* global variables */

mi pes[MAX_L + 1], invpes[MAX_L + 1], rhs[MAX_N];
char s[MAX_L + 4];
string ss[MAX_N];
mii lhs[MAX_L + 1][MAX_L];
int ls[MAX_N];

/* subroutines */

inline void prep_rhash(int n) {
  pes[0] = invpes[0] = 1;
  pes[1] = P;
  invpes[1] = pes[1].inv();
  for (int i = 2; i <= n; i++) {
    pes[i] = pes[i - 1] * P;
    invpes[i] = invpes[i - 1] * invpes[1];
  }
}

inline mi s2hash(const string &s) {
  mi h = 0;
  for (int k = 0; k < s.size(); k++) h += pes[k] * s[k];
  return h;
}

/* main */

int main() {
  prep_rhash(MAX_L);

  int n;
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    ss[i] = string(s);
    rhs[i] = s2hash(ss[i]);
    ls[i] = ss[i].size();

    for (int j = 0; j < ls[i]; j++) {
      mi h = rhs[i] - pes[j] * s[j];
      lhs[ls[i]][j][h.v]++;
    }
  }

  for (int i = 0; i < n; i++) {
    int li = ls[i], cnt = 0;
    for (int j = 0; j < li; j++) {
      mi h = rhs[i] - pes[j] * ss[i][j];
      cnt += lhs[li][j][h.v] - 1;
    }

    printf("%d\n", cnt);
  }
  
  return 0;
}
0