IOI 2001
In order to teach her young calvess the order of the letters in the alphabet, Bessie has come up with a game to play with them. The calves are given a 5 x 5 grid on which they can put the letters 'A'-'Y', but the one rule is that all the letters going across the columns and down the rows must be in the order they appear in the alphabet.
There are a huge number of possible grids, so Bessie decides that they will be named by the string of letters that is given by reading across each row, going down the rows. For example, the grid:
A B C D E F G H I J K L M N O P Q R S T U V W X Y
would have the name ABCDEFGHIJKLMNOPQRSTUVWXY, which is coincidentally the first possible grid when the entire set of grids is ordered alphabetically. The second grid that meets this requirement is ABCDEFGHIJKLMNOPQRSUTVWXY, which is formed by switching the 'T' and 'U' in the above grid.
Help the calves gain bragging rights. Given a number, M, find which string is Mth in the list of all possible grids when they are sorted alphabetically, and, given a string of letters, find out what number the corresponding grid is in the list of all possible grids.
PROGRAM NAME: twofive
INPUT FORMAT
The first input line contains one of two letters, either an 'N' or a 'W'.
If the first input line contains an 'N', the second line will contain an integer, M, that corresponds to the number of a valid grid. If the first line contains a 'W', the second line will contain a string of 25 letters, which represents a valid grid.
OUTPUT FORMAT
If the input contained the number of a valid grid (first line 'N'), the output should contain a string of 25 letters on a line, which corresponds to the Mth grid in the sorted list of all possible grids.
If the input contained a string of letters indicating a grid (first line 'W'), the output should contain a single integer on a line, which corresponds to the number of the given grid in the list of all possible grids.
SAMPLE INPUT #1 (file twofive.in)
N 2
SAMPLE OUTPUT #1 (file twofive.out)
ABCDEFGHIJKLMNOPQRSUTVWXY
SAMPLE INPUT #2 (file twofive.in)
W ABCDEFGHIJKLMNOPQRSUTVWXY
SAMPLE OUTPUT #2 (file twofive.out)
2
/*
ID: 15114582
PROG: twofive
LANG: C++
*/
#include<cstdio>
#include<cstring>
using namespace std;
int f[6][6][6][6][6];
char op[9],s[33],out[33];
int i,j,n,tmp;
bool ok(int x,int t)
{
return (!out[x]||out[x]==t+'A');
}
int dfs(int a,int b,int c,int d,int e,int t)
{
if(t>=25)return 1;
int &tmp=f[a][b][c][d][e];
if(tmp)return tmp;
if(a<5&&ok(a, t))tmp+=dfs(a+1,b,c,d,e,t+1);
if(b<a&&ok(b+ 5,t))tmp+=dfs(a,b+1,c,d,e,t+1);
if(c<b&&ok(c+10,t))tmp+=dfs(a,b,c+1,d,e,t+1);
if(d<c&&ok(d+15,t))tmp+=dfs(a,b,c,d+1,e,t+1);
if(e<d&&ok(e+20,t))tmp+=dfs(a,b,c,d,e+1,t+1);
return tmp;
}
int main()
{
freopen("twofive.in","r",stdin);
freopen("twofive.out","w",stdout);
while(~scanf("%s",op))
{
if(op[0]=='N')
{
scanf("%d",&n);
for(i=0;i<25;++i)
for(out[i]='A';1;++out[i])
{
memset(f,0,sizeof(f));
tmp=dfs(0,0,0,0,0,0);
if(tmp>=n)break;
n-=tmp;
}
puts(out);
}
if(op[0]=='W')
{
scanf("%s",s);
for(n=i=0;i<25;++i)
for(out[i]='A';out[i]<s[i];++out[i])
{
memset(f,0,sizeof(f));
n+=dfs(0,0,0,0,0,0);
}
printf("%d\n",n+1);
}
}
return 0;
}