結果

問題 No.563 超高速一人かるた large
ユーザー PulmnPulmn
提出日時 2017-08-18 19:33:01
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,405 bytes
コンパイル時間 1,193 ms
コンパイル使用メモリ 104,640 KB
実行使用メモリ 98,496 KB
最終ジャッジ日時 2023-08-04 17:48:00
合計ジャッジ時間 12,949 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
54,608 KB
testcase_01 AC 53 ms
54,688 KB
testcase_02 AC 54 ms
54,756 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 928 ms
54,752 KB
testcase_13 AC 931 ms
54,596 KB
testcase_14 WA -
testcase_15 AC 936 ms
54,732 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 114 ms
98,496 KB
testcase_20 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In member function ‘void Trie::dfs(int, vl&, ll&)’:
main.cpp:98:7: warning: ‘S’ may be used uninitialized in this function [-Wmaybe-uninitialized]
   int S;
       ^
main.cpp: In member function ‘void Trie::Solve()’:
main.cpp:98:7: warning: ‘S’ may be used uninitialized in this function [-Wmaybe-uninitialized]

ソースコード

diff #

#include <iostream>
#include <fstream>
#include <cassert>
#include <typeinfo>
#include <vector>
#include <stack>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <iomanip>
#include <cctype>
#include <random>
#include <complex>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<P,int> pip;
typedef vector<pip> vip;
const int inf=1<<29;
const ll INF=1ll<<58;
const double pi=acos(-1);
const double eps=1e-7;
const ll mod=1e9+7;
const int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};

ll Pow_mod(ll n,ll p){
	ll r=1;
	for(;p>0;p>>=1){
		if(p&1) r=(r*n)%mod;
		n=(n*n)%mod;
	}
	return r;
}

vl fact(3000);

ll Fact(ll n){
	if(fact[n]) return fact[n];
	if(!n) return fact[n]=1;
	return fact[n]=Fact(n-1)*n%mod;
}

ll Div(ll n,ll m){
	return n*Pow_mod(m,mod-2)%mod;
}

ll nCk(ll n,ll k){
	return Div(Fact(n),Fact(n-k)*Fact(k)%mod);
}

ll nPk(ll n,ll k){
	return Div(Fact(n),Fact(n-k));
}

int n;

class Trie{
	private:
	const int MAX=200005;
	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]+=nPk(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),dp_(u[v]+1);
				ll H=0;
				dfs(v_,DP,H);
/*				if(v==0){
					cout<<'B'<<H<<endl;
					for(int j=0;j<=n;j++) cout<<DP[j]<<' ';
					cout<<endl;
				}*/
				Fix(DP,H,v_);
				if(b){
					dp=DP;
					b=0;
					S=u[v_];
				}
				else{
/*					if(v==0) cout<<S<<' '<<u[v_]<<endl;
					if(v==0){
						cout<<'B';
						for(int j=0;j<=n;j++) cout<<DP[j]<<' ';
						cout<<endl;
					}*/
					for(int j=0;j<=S;j++) for(int k=0;k<=u[v_];k++){
//						if(v==0&&j==1&&k==1) cout<<'C'<<dp[j]<<' '<<DP[k]<<' '<<nPk(u[v_],k)<<' '<<nCk(j+k,j)<<endl;
						(dp_[j+k]+=(nPk(u[v_],k)*dp[j]%mod+nPk(S,j)*DP[k]%mod)*nCk(j+k,j))%=mod;
					}
					S+=u[v_];
					dp=dp_;
				}
/*				if(v==0){
					cout<<'A';
					for(int i=0;i<=n;i++){
						cout<<dp[i]<<' ';
					}
					cout<<endl;
				}*/
			}
		}
		for(int i=1;i<=u[v];i++) (dp[i]+=(i-(i==u[v]?1:0))*nPk(u[v],i))%=mod;
/*		cout<<'V'<<v<<endl;
		for(int i=0;i<=n;i++) cout<<dp[i]<<' ';
		cout<<endl;*/
	}
	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(){
		vl dp(n+1);
		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))*nPk(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