Time Limit: 1 s
Description
Corn does not participate the STEED contest, but he is interested in the word "STEED". So, Corn writes all permutations of the word "STEED" on different cards and gets 60 cards finally.
Corn sorts these cards in
lexicographical order
, and marks them from 1 to 60.
Now, Corn gives you a integer
N
(1 ≤
N
≤ 60), could you tell him the word on the
N
th
card?
Input
There are multiple test cases (no more than 60).
For each test case, there is one integer
N (1 ≤
N ≤ 60) in a line.
Output
For each test case, you should output one line with the word on the Nth card.
Sample Input
1 2 3 4 47 48 49
Sample Output
DEEST DEETS DESET DESTE STEDE STEED TDEES
本题说是给你五个字母STEED让你按字典序全排列,重复的只出现一次。给你一个n,输出第n个字符串。
解题思路:
用STL中的全排列函数 next_permutation。
注意:
字典序排序序号是从1开始的。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string.h>
#include <map>
using namespace std;
string s[65];
void pailie()
{
char str[10];
str[0]='D';
str[1]='E';
str[2]='E';
str[3]='S';
str[4]='T';
int j=1;
map<string,int>M;
do
{
string ss="";
for(int i=0; i<5; i++)
{
ss+=str[i];
}
if(M[ss]==0)///判断字符串是否出现过
{
M[ss]=j;
s[j]=ss;
j++;
}
}while(next_permutation(str,str+5));
}
int main()
{
pailie();
int n;
while(~scanf("%d",&n))
{
cout<<s[n]<<endl;
}
return 0;
}
下边来说一种最开始不知道全排列函数是的暴力列举的代码。由于字母数不多,所以暴力也过了。
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
int main()
{
int i;
string s[60];
s[0]="DEEST";
s[1]="DEETS";
s[2]="DESET";
s[3]="DESTE";
s[4]="DETES";
s[5]="DETSE";
s[6]="DSEET";
s[7]="DSETE";
s[8]="DSTEE";
s[9]="DTEES";
s[10]="DTESE";
s[11]="DTSEE";
s[12]="EDEST";
s[13]="EDETS";
s[14]="EDSET";
s[15]="EDSTE";
s[16]="EDTES";
s[17]="EDTSE";
s[18]="EEDST";
s[19]="EEDTS";
s[20]="EESDT";
s[21]="EESTD";
s[22]="EETDS";
s[23]="EETSD";
s[24]="ESDET";
s[25]="ESDTE";
s[26]="ESEDT";
s[27]="ESETD";
s[28]="ESTDE";
s[29]="ESTED";
s[30]="ETDES";
s[31]="ETDSE";
s[32]="ETEDS";
s[33]="ETESD";
s[34]="ETSDE";
s[35]="ETSED";
s[36]="SDEET";
s[37]="SDETE";
s[38]="SDTEE";
s[39]="SEDET";
s[40]="SEDTE";
s[41]="SEEDT";
s[42]="SEETD";
s[43]="SETDE";
s[44]="SETED";
s[45]="STDEE";
s[46]="STEDE";
s[47]="STEED";
s[48]="TDEES";
s[49]="TDESE";
s[50]="TDSEE";
s[51]="TEDES";
s[52]="TEDSE";
s[53]="TEEDS";
s[54]="TEESD";
s[55]="TESDE";
s[56]="TESED";
s[57]="TSDEE";
s[58]="TSEDE";
s[59]="TSEED";
while(~scanf("%d",&i))
cout<<s[i-1]<<endl;
}