結果

問題 No.563 超高速一人かるた large
ユーザー koba-e964koba-e964
提出日時 2017-08-25 23:28:51
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 2,423 bytes
コンパイル時間 1,326 ms
コンパイル使用メモリ 115,356 KB
実行使用メモリ 19,712 KB
最終ジャッジ日時 2024-04-23 15:46:25
合計ジャッジ時間 4,320 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 AC 6 ms
6,940 KB
testcase_20 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
const ll mod = 1e9 + 7;

const int N = 2437;
ll fact[N];
void init(void) {
  fact[0] = 1;
  REP(i, 1, N) {
    fact[i] = fact[i - 1] * i % mod;
  }
}

vector<VI> get_lcp(const vector<pair<string, int> > &pool) {
  int n = pool.size();
  vector<VI> lcp(n, VI(n));
  VI neighbor(n - 1);
  REP(i, 0, n - 1) {
    const string& u = pool[i].first;
    const string& v = pool[i + 1].first;
    int idx = 0;
    while (true) {
      if ((int) u.length() <= idx || (int) v.length() <= idx) {
	break;
      }
      if (u[idx] == v[idx]) {
	idx += 1;
      } else {
	break;
      }
    }
    neighbor[i] = idx;
  }
  REP(i, 0, n - 1) {
    int cur = 1e8;
    REP(j, i + 1, n) {
      cur = min(cur, neighbor[j - 1]);
      lcp[pool[i].second][pool[j].second] = cur;
      lcp[pool[j].second][pool[i].second] = cur;
    }
  }
  return lcp;
}

int main(void) {
  init();
  int n;
  cin >> n;
  vector<string> s(n);
  vector<pair<string, int> > pool(n);
  REP(i, 0, n) {
    cin >> s[i];
    s[i] += '$';
    pool[i] = make_pair(s[i], i);
  }
  sort(pool.begin(), pool.end());
  vector<VI> lcp = get_lcp(pool);
  assert (n <= 20);
  vector<ll> dp(1 << n);
  dp[0] = 0;
  REP(bits, 1, 1 << n) {
    int k = __builtin_popcount(bits) - 1;
    ll fac = fact[k];
    ll tot = 0;
    vector<int> remaining;
    REP(i, 0, n) {
      if ((bits & 1 << i) == 0) {
	remaining.push_back(i);
      }
    }
    REP(i, 0, n) {
      if ((bits & 1 << i) == 0) {
	continue;
      }
      int idx = 1;
      REP(j, 0, remaining.size()) {
	int jidx = remaining[j];
	idx = max(idx, lcp[jidx][i] + 1);
      }
      // cerr << "tt" << bits << " " << i << " " << idx << endl;
      tot += dp[bits ^ 1 << i] + idx * fac % mod;
    }
    dp[bits] = tot % mod;
  }
  VL tbl(n + 1, 0);
  REP(bits, 0, 1 << n) {
    tbl[__builtin_popcount(bits)] += dp[bits];
  }
  REP(i, 1, n + 1) {
    cout << tbl[i] % mod << endl;
  }
}
0