提出 #486952
ソースコード 拡げる
#include<cstdio>
#include<cassert>
using namespace std;
bool dp[1<<16][16][6];
char to[1<<16][16][6];
int nrem[1<<16][16][6];
bool check[1<<16][16][6];
bool can[16][6][16];
char s[16][20];
int n,k;
int fac[4]={1,1,2,6};
void decode(char *res,int pre,int rem){
bool used[20]={};
for(int i=0;i<k;i++) res[i]=s[pre][k-1-i],used[res[i]-'A']=true;
for(int i=n-k-1;i>=0;i--){
int now=rem/fac[i];
rem%=fac[i];
int j;
for(j=0;j<now;j++){
if(used[j]) now++;
}
while(used[j]) j++;
res[i+k]='A'+j;
used[j]=true;
}
}
int encode(char *remc){
int sz=n-k,res=0;
for(int i=sz-1;i>=0;i--){
int cnt=0;
for(int j=i-1;j>=0;j--){
if(remc[j]<remc[i]) cnt++;
}
res+=cnt*fac[i];
}
return res;
}
void build(){
char tmp[20];
for(int a=0;a<n;a++){
for(int b=0;b<fac[n-k];b++){
decode(tmp,a,b);
for(int c=0;c<n;c++){
int now=0;
bool flag=true;
for(int d=0;d<k;d++){
while(now<n&&tmp[now]!=s[c][d]) now++;
if(now==n) flag=false;
else now++;
}
can[a][b][c]=flag;
}
}
}
}
bool f(int stat,int pre,int rem){
if(!stat) return true;
if(check[stat][pre][rem]) return dp[stat][pre][rem];
check[stat][pre][rem]=true;
if(stat==(1<<n)-1){
for(int i=0;i<n;i++){
for(int j=0;j<6;j++){
if(f(stat^(1<<i),i,j)){
to[stat][pre][rem]='A'+i;
nrem[stat][pre][rem]=j;
return dp[stat][pre][rem]=true;
}
}
}
return dp[stat][pre][rem]=false;
}
char tmp[20];
decode(tmp,pre,rem);
tmp[n]=0;
for(int i=0;i<n;i++){
if((stat&(1<<i))&&can[pre][rem][i]){
bool used[20]={};
char remc[3];
int sz=0;
for(int j=0;j<k;j++){
used[s[i][j]-'A']=true;
}
for(int j=0;j<n;j++){
if(!used[tmp[j]-'A']) remc[sz++]=tmp[j];
}
if(f(stat^(1<<i),i,encode(remc))){
to[stat][pre][rem]='A'+i;
nrem[stat][pre][rem]=encode(remc);
return dp[stat][pre][rem]=true;
}
}
}
return dp[stat][pre][rem]=false;
}
int main(){
scanf("%d%d",&n,&k);
for(int i=0;i<n;i++){
scanf("%s",s[i]);
}
build();
assert(f((1<<n)-1,0,0));
int a=(1<<n)-1,b=0,c=0;
for(int i=0;i<n;i++){
putchar(to[a][b][c]);
int na=a^(1<<to[a][b][c]-'A');
int nb=to[a][b][c]-'A';
int nc=nrem[a][b][c];
a=na,b=nb,c=nc;
}
putchar('\n');
return 0;
}
提出情報
提出日時
2015-09-06 05:47:51+0900
問題
G - JAG-channel II
ユーザ
NCTU_Thor
言語
C++ (GCC 4.9.2)
得点
100
コード長
2335 Byte
結果
AC
実行時間
336 ms
メモリ
11948 KiB
コンパイルエラー
./Main.cpp: In function ‘int main()’:
./Main.cpp:96:21: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&k);
^
./Main.cpp:98:19: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%s",s[i]);
^
ジャッジ結果
セット名
All
得点 / 配点
100 / 100
結果
セット名
テストケース
All
00_sample_00, 00_sample_01, 00_sample_02, 10_handmade_00, 10_handmade_01, 10_handmade_02, 20_random_00, 20_random_01, 20_random_02, 20_random_03, 20_random_04, 20_random_05, 20_random_06, 20_random_07, 20_random_08, 20_random_09, 20_random_10, 20_random_11, 20_random_12, 20_random_13, 20_random_14, 20_random_15, 20_random_16, 20_random_17, 20_random_18, 20_random_19, 30_worst_00, 30_worst_01, 30_worst_02, 30_worst_03, 30_worst_04, 30_worst_05, 30_worst_06, 30_worst_07, 30_worst_08, 30_worst_09, 30_worst_10, 30_worst_11, 30_worst_12, 30_worst_13, 30_worst_14, 30_worst_15
ケース名
結果
実行時間
メモリ
00_sample_00
AC
24 ms
672 KiB
00_sample_01
AC
21 ms
804 KiB
00_sample_02
AC
23 ms
1308 KiB
10_handmade_00
AC
22 ms
800 KiB
10_handmade_01
AC
22 ms
928 KiB
10_handmade_02
AC
110 ms
6952 KiB
20_random_00
AC
22 ms
928 KiB
20_random_01
AC
21 ms
796 KiB
20_random_02
AC
23 ms
916 KiB
20_random_03
AC
24 ms
928 KiB
20_random_04
AC
25 ms
1316 KiB
20_random_05
AC
23 ms
1048 KiB
20_random_06
AC
21 ms
796 KiB
20_random_07
AC
21 ms
800 KiB
20_random_08
AC
25 ms
1312 KiB
20_random_09
AC
23 ms
796 KiB
20_random_10
AC
23 ms
804 KiB
20_random_11
AC
24 ms
1180 KiB
20_random_12
AC
21 ms
692 KiB
20_random_13
AC
24 ms
796 KiB
20_random_14
AC
23 ms
792 KiB
20_random_15
AC
21 ms
800 KiB
20_random_16
AC
25 ms
1056 KiB
20_random_17
AC
22 ms
924 KiB
20_random_18
AC
22 ms
1056 KiB
20_random_19
AC
25 ms
1060 KiB
30_worst_00
AC
24 ms
928 KiB
30_worst_01
AC
120 ms
11680 KiB
30_worst_02
AC
165 ms
11676 KiB
30_worst_03
AC
194 ms
11684 KiB
30_worst_04
AC
201 ms
11948 KiB
30_worst_05
AC
210 ms
11808 KiB
30_worst_06
AC
213 ms
8472 KiB
30_worst_07
AC
202 ms
8352 KiB
30_worst_08
AC
206 ms
7460 KiB
30_worst_09
AC
274 ms
7328 KiB
30_worst_10
AC
303 ms
7076 KiB
30_worst_11
AC
321 ms
7080 KiB
30_worst_12
AC
327 ms
6940 KiB
30_worst_13
AC
336 ms
6944 KiB
30_worst_14
AC
325 ms
6948 KiB
30_worst_15
AC
329 ms
6944 KiB