結果

問題 No.563 超高速一人かるた large
ユーザー PulmnPulmn
提出日時 2017-08-18 22:50:26
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 1,837 bytes
コンパイル時間 742 ms
コンパイル使用メモリ 67,500 KB
実行使用メモリ 48,896 KB
最終ジャッジ日時 2024-04-22 15:53:52
合計ジャッジ時間 3,932 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
48,896 KB
testcase_01 AC 39 ms
48,896 KB
testcase_02 AC 39 ms
48,896 KB
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 -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In member function ‘void Trie::dfs(int, vl&, ll&)’:
main.cpp:46:21: warning: ‘S’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   46 |                 int S;
      |                     ^

ソースコード

diff #

#include <iostream>
#include <cassert>
#include <vector>
#include <string>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
const ll mod=1e9+7;

const ll M=2005;

ll C[M][M],P[M][M];

void Init(){
	C[0][0]=P[0][0]=1;
	for(int i=0;i<M;i++) for(int j=0;j<=i;j++) if(i||j){
		if(i>j) C[i][j]=C[i-1][j];
		if(i&&j) (C[i][j]+=C[i-1][j-1])%=mod;
		if(j) P[i][j]=P[i-1][j-1]*i%mod;
		else P[i][j]=1;
	}
}

int n;

class Trie{
	private:
	const int MAX=205;
	int ID=1;
	vvl date;
	vl a,u,dp;
	void Fix(vl& dp,ll h,int v){
		if(u[v]==1){
			dp[1]=1;
			return;
		}
		for(int i=1;i<=u[v];i++) (dp[i]+=P[u[v]][i]%mod*h)%=mod;
	}
	void dfs(int v,vl& dp,ll& h){
		if(a[v]==1){
			for(int i=0;i<27;i++) if(date[v][i]!=-1) dfs(date[v][i],dp,h);
			h++;
			return;
		}
		int S;
		bool b=1;
		for(int i=0;i<27;i++){
			int v_=date[v][i];
			if(v_!=-1){
				vl DP(u[v_]+1);
				ll H=0;
				dfs(v_,DP,H);
				Fix(DP,H,v_);
				if(b){
					dp=DP;
					b=0;
					S=u[v_];
				}
				else{
					vl dp_(S+u[v_]+1);
					for(int j=0;j<=S;j++) for(int k=0;k<=u[v_];k++) (dp_[j+k]+=(P[u[v_]][k]*dp[j]%mod+P[S][j]*DP[k]%mod)*C[j+k][j])%=mod;
					S+=u[v_];
					dp=dp_;
				}
			}
		}
		if(!a[v]) dp=vl(2);
		for(int i=1;i<=u[v];i++) (dp[i]+=(i-(i==u[v]?1:0))*P[u[v]][i])%=mod;
	}
	public:
	Trie(){
		date=vvl(MAX,vl(27,-1));
		a=u=vl(MAX);
	}
	void Update(string s){
		int S=s.size(),I=0;
		u[0]++;
		for(int i=0;i<S;i++){
			int c=s[i]-'a';
			if(date[I][c]==-1){
				date[I][c]=ID++;
				a[I]++;
			}
			I=date[I][c];
			u[I]++;
		}
	}
	void Solve(){
		Init();
		vl dp;
		ll h=0;
		dfs(0,dp,h);
		Fix(dp,h,0);
		for(int i=1;i<=n;i++) cout<<(dp[i]-(i-(i==n?1:0))*P[n][i]%mod+mod)%mod<<endl;
	}
};

int main(){
	cin>>n;
	Trie trie;
	for(int i=0;i<n;i++){
		string s;
		cin>>s;
		s+="{";
		trie.Update(s);
	}
	trie.Solve();
}
0