提出 #15780880


ソースコード 拡げる

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>

using namespace std;

typedef long long ll;

const ll MOD = 3006703054056749LL;

struct Modint {
  ll val;
  
  Modint (ll _val = 0)
    : val(_val % MOD) {}

  Modint operator+ (Modint other) const {
    return Modint(val + other.val);
  }

  void operator+= (Modint other) {
    val += other.val;
    val %= MOD;
  }

  Modint operator- () const {
    return Modint(MOD - val);
  }

  Modint operator- (Modint other) const {
    return Modint(val + MOD - other.val);
  }

  void operator-= (Modint other) {
    val += MOD - other.val;
    val %= MOD;
  }

  Modint operator* (Modint other) const {
    return Modint(val * other.val);
  }

  void operator*= (Modint other) {
    val *= other.val;
    val %= MOD;
  }

  bool operator== (Modint other) const {
    return val == other.val;
  }

  bool operator!= (Modint other) const {
    return val != other.val;
  }
};

ostream& operator<< (ostream& out, Modint p) {
  out << p.val;
  return out;
}

const int MAX_N = 2e5 + 5;
const Modint BASE (231);

ll ans;
string s [MAX_N];
vector<int> rewards [MAX_N];
map<ll, int> fst;

bool has (const set<pair<char, int>> &exist, char val) {
  auto lb = exist.lower_bound(make_pair(val, -1));
  if (lb == exist.end()) return false;
  return lb->first == val;
}

void proc (int idx) {
  int n = s[idx].size();
  set<pair<char, int>> exist;
  for (int i = 0; i < n; i++) {
    exist.insert(make_pair(s[idx][i], i));
  }

  Modint hsh (0);
  if (fst.count(hsh.val)) {
    int udx = fst[hsh.val];
    for (int c = 0; c < 26; c++) {
      if (has(exist, c + 'a')) {
        ans += rewards[udx][c];
      }
    }
  }

  for (int i = n - 1; i > 0; i--) {
    hsh *= BASE;
    hsh += Modint(s[idx][i]);
    exist.erase(make_pair(s[idx][i], i));

    if (fst.count(hsh.val)) {
      int udx = fst[hsh.val];
      for (int c = 0; c < 26; c++) {
        if (has(exist, c + 'a')) {
          ans += rewards[udx][c];
        }
      }
    }
  }

  if (!fst.count(hsh.val)) {
    fst[hsh.val] = idx;
    rewards[idx] = vector<int> (26, 0);
  }
  rewards[fst[hsh.val]][s[idx][0] - 'a']++;
}

int main () {
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  vector<pair<int, int>> lengths;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    lengths.push_back(make_pair((int) s[i].size(), i));
  }

  sort(lengths.begin(), lengths.end());

  for (auto pr : lengths) {
    proc(pr.second);
  }

  cout << ans << endl;
}

提出情報

提出日時
問題 B - First Second
ユーザ is_this_fft
言語 C++ (GCC 9.2.1)
得点 700
コード長 2648 Byte
結果 AC
実行時間 600 ms
メモリ 61396 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 700 / 700
結果
AC × 2
AC × 26
セット名 テストケース
Sample s1.txt, s2.txt
All 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, s1.txt, s2.txt
ケース名 結果 実行時間 メモリ
001.txt AC 16 ms 14512 KiB
002.txt AC 21 ms 14508 KiB
003.txt AC 13 ms 14512 KiB
004.txt AC 43 ms 15972 KiB
005.txt AC 354 ms 23516 KiB
006.txt AC 410 ms 61396 KiB
007.txt AC 403 ms 53540 KiB
008.txt AC 536 ms 39316 KiB
009.txt AC 490 ms 25696 KiB
010.txt AC 378 ms 18564 KiB
011.txt AC 276 ms 15764 KiB
012.txt AC 488 ms 29040 KiB
013.txt AC 471 ms 38312 KiB
014.txt AC 487 ms 43464 KiB
015.txt AC 355 ms 19980 KiB
016.txt AC 389 ms 16256 KiB
017.txt AC 339 ms 15804 KiB
018.txt AC 371 ms 15924 KiB
019.txt AC 557 ms 16112 KiB
020.txt AC 600 ms 35320 KiB
021.txt AC 452 ms 22084 KiB
022.txt AC 317 ms 16892 KiB
023.txt AC 210 ms 15984 KiB
024.txt AC 322 ms 16432 KiB
s1.txt AC 16 ms 14556 KiB
s2.txt AC 14 ms 14540 KiB