ECNU Online Judge11月赛 纸条(思维+进制)

题目链接https://acm.ecnu.edu.cn/contest/231/problem/A/
在这里插入图片描述
分析
每一个#号都有k个候选字母。把k个字母编号0~k-1。
比如每一个#号三个候选字母。
按字典序输出就是
00
01
02
10
11
12
可以把x转化为k进制。
看每一个#号该输出候选单词的哪一个字母。
代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N=5*1e5+10;
int n,m,k;
LL x;
char a[N];
char ch[N][30];
int sys[N],tot;
int ans[N];
int main()
{
    scanf("%d%d%d%lld",&n,&m,&k,&x);
    scanf("%s",a);
    for(int i=0; i<m; i++)
    {
        scanf("%s",ch[i]);
        sort(ch[i],ch[i]+k);
    }
    x-=1;
    while(x)/*x用k进制表示*/
    {
        sys[tot++]=x%k;
        x/=k;
    }
    int u=0;
    for(int i=m-1; i>=0; i--) /*每一个#用给定单词的那个位置的字母*/
        ans[u++]=sys[i];
    int book=0;
    for(int i=0; i<n; i++)
    {
        if(a[i]!='#')
            printf("%c",a[i]);
        else
        {
            printf("%c",ch[book][ans[book]]);
            book++;
        }
    }
    printf("\n");
    return 0;
}
发布了165 篇原创文章 · 获赞 6 · 访问量 5066

猜你喜欢

转载自blog.csdn.net/lylzsx20172018/article/details/103322055