結果

問題 No.562 超高速一人かるた small
ユーザー koyumeishikoyumeishi
提出日時 2017-06-18 15:19:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 399 ms / 3,000 ms
コード長 1,931 bytes
コンパイル時間 1,512 ms
コンパイル使用メモリ 106,440 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-23 16:08:04
合計ジャッジ時間 4,902 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 14 ms
5,376 KB
testcase_09 AC 399 ms
5,376 KB
testcase_10 AC 157 ms
5,376 KB
testcase_11 AC 368 ms
5,376 KB
testcase_12 AC 23 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 152 ms
5,376 KB
testcase_15 AC 155 ms
5,376 KB
testcase_16 AC 108 ms
5,376 KB
testcase_17 AC 168 ms
5,376 KB
testcase_18 AC 164 ms
5,376 KB
testcase_19 AC 190 ms
5,376 KB
testcase_20 AC 184 ms
5,376 KB
testcase_21 AC 165 ms
5,376 KB
testcase_22 AC 217 ms
5,376 KB
testcase_23 AC 166 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
O(2^n * n) 探索
*/

#include <iostream>
#include <string>
#include <vector>
#include <cassert>
#include <random>
#include <algorithm>
#include <set>
#include <chrono>
using namespace std;

void timer(){
  static chrono::steady_clock::time_point start = chrono::steady_clock::now();
  auto now = chrono::steady_clock::now();
  cerr << "elapsed time : " << chrono::duration_cast<chrono::milliseconds>(now-start).count() << "[ms]" << endl;
}

const long long mod = 1000000007;

long long ans[20];
long long lcp[22][22];
vector<string> s;

int get_lcp_len(int x, int y){
  if( lcp[x][y] != -1 ) return lcp[x][y];
  string& a = s[x];
  string& b = s[y];
  for(int i=0; i<a.size() && i<b.size(); i++){
    if( a[i] != b[i] ) return i;
  }
  return lcp[x][y] = lcp[y][x] = min(a.size(), b.size());
}

long long mod_pow(long long x, long long y, long long mod){  //(x^y) % mod
  if(x==0 && y!=0) return 0;
  long long ret=1LL;
  while(y>0LL){
    if(y&1LL) ret = (ret * x) % mod;
    x = (x*x) % mod;
    y >>= 1LL;
  }
  return ret;
}
long long fact[21];

int main(){
  timer();

  fact[0] = 1;
  for(int i=1; i<=20; i++){
    fact[i] = fact[i-1] * i % mod;
  }
  for(int i=0; i<22; i++){
    for(int j=0; j<22; j++){
      lcp[i][j] = -1;
    }
  }

  int n;
  cin >> n;
  assert( n<=20 );
  
  s = vector<string>(n);
  for(int i=0; i<n; i++) cin >> s[i];
  s.push_back(string(1, 'a'-1));
  s.push_back(string(1, 'z'+1));

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

  for(int i=1; i<(1<<n); i++){
    long long tmp = 0;
    int pre = 0;
    for(int j=0; j<n; j++){
      if( (~i>>j)&1 ){
        pre = j+1;
        continue;
      }
      int nxt = j+2;

      int len_a = get_lcp_len( j+1, pre );
      int len_b = get_lcp_len( j+1, nxt );
      tmp += max(len_a, len_b) + 1;
    }
    int k = __builtin_popcount(i);
    (ans[ k-1 ] += tmp ) %= mod;
  }

  for(int i=0; i<n; i++) cout << (ans[i]*fact[i+1])%mod << endl;

  timer();
  return 0;
}
0