結果

問題 No.562 超高速一人かるた small
ユーザー komori3komori3
提出日時 2017-08-26 00:19:29
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 2,031 bytes
コンパイル時間 797 ms
コンパイル使用メモリ 71,376 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-23 16:25:03
合計ジャッジ時間 4,679 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
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 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long lint;

#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MOD ((lint)1000000007)

template <typename _Ty>
ostream& operator << (ostream& ostr, const vector<_Ty>& v) {
	if (v.empty()) {
		cout << "{ }";
		return ostr;
	}
	cout << "{" << v.front();
	for (auto itr = ++v.begin(); itr != v.end(); itr++) {
		cout << ", " << *itr;
	}
	cout << "}";
	return ostr;
}

/*
6
abc
abcedaa
abcdefg
abvdegf
aaa
abceg

*/

lint ipow(lint x, lint n)
{
	lint i, ret = 1;
	for (i = 0; i<n; i++)
		ret *= x;
	return ret;
}

lint Combination(lint n, lint r)
{
	lint i, ret = 1;
	r = MIN(n - r, r);
	for (i = 1; i <= r; i++) {
		ret = ret * (n - i + 1) / i;
	}
	return ret % MOD;
}

lint common_seq(const string& a, const string& b)
{
	lint i;
	for (i = 0; i < MIN(a.size(), b.size()); i++) {
		if (a[i] != b[i]) break;
	}
	return i;
}

//bits : 残っているかるた
lint fatigue(vector<string>& S, lint bits, lint N)
{
	lint fatigue = 0;
	vector<string> sub;
	for (int i = 0; bits != 0, i < N; i++) {
		if (bits % 2) sub.push_back(S[i]);
		bits /= 2;
	}
	if (sub.size() == 1) fatigue = 1;
	else {
		fatigue += common_seq(sub[0], sub[1]) + 1;
		for (lint i = 1; i < sub.size() - 1; i++) {
			fatigue += MAX(common_seq(sub[i - 1], sub[i]), common_seq(sub[i], sub[i + 1])) + 1;
		}
		if (sub.size() != 2) {
			fatigue += common_seq(sub[sub.size() - 2], sub[sub.size() - 1]) + 1;
		}
	}

	lint prob = Combination(N, sub.size()) * fatigue % MOD;
	return prob;
}

int main()
{
	lint N, pw, E[21] = { 0 };
	cin >> N;
	vector<string> S(N);
	for (lint i = 0; i < N; i++) {
		cin >> S[i];
		//S[i].push_back('0');
	}
	sort(S.begin(), S.end());
	//cout << S << endl;
	pw = ipow(2, N);
	for (int b = 0; b < pw; b++) {
		lint nb = 0;
		for (int i = 1; i <= (1 << 20); i <<= 1) {
			if (b & 1) nb++;
		}
		E[nb] += fatigue(S, b, N);
		E[nb] %= MOD;
	}
	for (int i = 0; i < N; i++)
		cout << E[i] << endl;

	return 0;
}
0