結果

問題 No.562 超高速一人かるた small
ユーザー startcppstartcpp
提出日時 2017-08-26 15:50:40
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 886 ms / 3,000 ms
コード長 1,625 bytes
コンパイル時間 542 ms
コンパイル使用メモリ 59,316 KB
実行使用メモリ 11,648 KB
最終ジャッジ日時 2024-04-23 17:10:43
合計ジャッジ時間 11,508 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,948 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 5 ms
6,940 KB
testcase_08 AC 86 ms
6,940 KB
testcase_09 AC 881 ms
11,520 KB
testcase_10 AC 189 ms
6,940 KB
testcase_11 AC 407 ms
7,424 KB
testcase_12 AC 85 ms
6,940 KB
testcase_13 AC 3 ms
6,940 KB
testcase_14 AC 862 ms
11,520 KB
testcase_15 AC 859 ms
11,520 KB
testcase_16 AC 875 ms
11,520 KB
testcase_17 AC 860 ms
11,392 KB
testcase_18 AC 862 ms
11,520 KB
testcase_19 AC 875 ms
11,520 KB
testcase_20 AC 876 ms
11,392 KB
testcase_21 AC 864 ms
11,520 KB
testcase_22 AC 866 ms
11,648 KB
testcase_23 AC 886 ms
11,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#define int long long
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;
}

signed 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;
		fact[i] %= 1000000007;
	}
	
	//遷移前 < 遷移後なので、値が小さい状態から調べればよい。
	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