結果

問題 No.562 超高速一人かるた small
ユーザー koba-e964koba-e964
提出日時 2017-08-25 23:29:46
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 467 ms / 3,000 ms
コード長 2,423 bytes
コンパイル時間 1,252 ms
コンパイル使用メモリ 115,036 KB
実行使用メモリ 11,520 KB
最終ジャッジ日時 2024-04-23 16:16:10
合計ジャッジ時間 7,593 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 5 ms
5,376 KB
testcase_08 AC 55 ms
5,376 KB
testcase_09 AC 463 ms
11,392 KB
testcase_10 AC 110 ms
5,376 KB
testcase_11 AC 224 ms
7,296 KB
testcase_12 AC 54 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 457 ms
11,392 KB
testcase_15 AC 465 ms
11,392 KB
testcase_16 AC 465 ms
11,376 KB
testcase_17 AC 463 ms
11,392 KB
testcase_18 AC 464 ms
11,520 KB
testcase_19 AC 465 ms
11,412 KB
testcase_20 AC 463 ms
11,392 KB
testcase_21 AC 467 ms
11,392 KB
testcase_22 AC 461 ms
11,392 KB
testcase_23 AC 465 ms
11,392 KB
権限があれば一括ダウンロードができます

ソースコード

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