一般我们在对字符串排序时,都会按照字典序排序。当字符串只包含小写字母时,相当于按字母表"abcdefghijklmnopqrstuvwxyz"的顺序排序。
现在我们打乱字母表的顺序,得到一个26个字母的新顺序。例如"bdceafghijklmnopqrstuvwxyz"代表'b'排在'd'前,'d'在'c'前,'c'在'e'前……
给定N个字符串,请你按照新的字母顺序对它们排序。
Input
第一行包含一个整数N。(1 <= N <= 1000)
第二行包含26个字母,代表新的顺序。
以下N行每行一个字符串S。 (|S| <= 100)
Output
按新的顺序输出N个字符串,每个字符串一行。
Sample Input
5 bdceafghijklmnopqrstuvwxyz abcde adc cda cad ddc
Sample Output
ddc cda cad abcde adc
#include<bits/stdc++.h>
using namespace std;
const int N=1e3+3;
char t[N],s[N];
int b[N];
struct dd
{
int id;
char ss[N];
}w[N];
bool cmp(dd q,dd p)
{
if(strcmp(q.ss,p.ss)!=0)
return strcmp(q.ss,p.ss)<=0;
else return q.id<p.id;
}
int main()
{
int i,j,k=0;
int n,m,l;
scanf("%d%s",&n,s);
l=strlen(s);
for(i=0;i<l;i++) b[s[i]]=i;
for(i=0;i<n;i++)
{
scanf("%s",t);
l=strlen(t);
for(int j=0;j<l;j++) t[j]=b[t[j]]+'a';//转换
strcpy(w[k].ss,t);
w[k++].id=k;
}
sort(w,w+n,cmp);
for(i=0;i<n;i++)
{
l=strlen(w[i].ss);
for(int j=0;j<l;j++)
printf("%c",s[w[i].ss[j]-'a']);
printf("\n");
}
return 0;
}