結果

問題 No.562 超高速一人かるた small
ユーザー startcppstartcpp
提出日時 2017-08-26 15:48:29
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,568 bytes
コンパイル時間 473 ms
コンパイル使用メモリ 59,604 KB
実行使用メモリ 7,552 KB
最終ジャッジ日時 2024-04-23 17:10:24
合計ジャッジ時間 10,044 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int n;
string s[20];
int pre[20][20];	//pre[i][j] = 文字列s[i], s[j]の共通prefixの長さ
int dp[1 << 20];	//dp[i] = i…既に読んだ読み札の集合, dp[i]…そこまでの全読み方における疲労度の総和
int fact[20];

int getPreNum(string &s, string &t) {
	int n = min(s.length(), t.length());
	for (int i = 0; i < n; i++) {
		if (s[i] != t[i]) {
			return i;
		}
	}
	return n;
}

int bitCount(int x) {
	int cnt = 0;
	while (x > 0) {
		cnt += (x & 1);
		x >>= 1;
	}
	return cnt;
}

int main() {
	int i, j, k;
	
	cin >> n;
	for (i = 0; i < n; i++) cin >> s[i];
	
	for (i = 0; i < n; i++) {
		for (j = 0; j < n; j++) {
			pre[i][j] = getPreNum(s[i], s[j]);
		}
	}
	
	fact[0] = 1;
	for (i = 1; i < n; i++) fact[i] = fact[i - 1] * i;
	
	//遷移前 < 遷移後なので、値が小さい状態から調べればよい。
	for (i = 0; i < (1 << n) - 1; i++) {
		for (j = 0; j < n; j++) {
			if ((i >> j) & 1) continue;
			//読み札jを読む
			int tired = 0;
			for (k = 0; k < n; k++) {
				if (k != j && ((i >> k) & 1) == 0) {
					tired = max(tired, pre[j][k]);
				}
			}
			tired++;
			//遷移コスト = tired * その状態に至る経路の個数(=bitCount(i)!)
			dp[i + (1 << j)] += dp[i] + tired * fact[bitCount(i)];
			dp[i + (1 << j)] %= 1000000007;
		}
	}

	int ans[21] = {0};
	for (i = 0; i < (1 << n); i++) {
		ans[bitCount(i)] += dp[i];
		ans[bitCount(i)] %= 1000000007;
	}
	
	for (i = 1; i <= n; i++) {
		cout << ans[i] << endl;
	}
	return 0;
}
0