音乐播放器
chu_K's blog
 
文章 标签
22 9

Powered by Gridea | Theme: Fog
载入天数...
载入时分秒...
御风飞行中...

【OI】题解 P6495 [COCI2016-2017#2] Tavan

Sol:

是一道简单的字符串题。

题目大意:给你一个由 # 与小写字母组成的字符串,每个 #kk 种字母可填,试求所有方案字典序排列后的第 xx 种方案。

因为是按字典序排列,所以我们可以将 mm 个长度为 kk 的字符串一一排序。然后找每个待更新的 # 要更新的字符的位置,最后更新字符串即可。

建议黄。

Code:

#include <cmath>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <iostream>
#define int long long
using namespace std;
int x,m,k,n,kk,kkk;
int a[501],d[501];
char s[501],ss[501][501];

signed main()
{
        cin >> n >> m >> k >> x;
        x--;
        kk=0;
        scanf("%s",s+1);
        for(int i=1; i<=n; i++)
                if(s[i]=='#')
                        d[kk++]=i;//d数组表示#的位置。

        for(int i=0; i<m; i++)
        {
                scanf("%s",ss[i]);
                sort(ss[i],ss[i]+k);
        }//因为按字典序,所以要排序。

        kk=0; kkk=1;//kkk就是位值。
        while(kkk*k<=x)
                kkk=k*kkk,kk++;

        while(x && kk>=0)
        {
                a[kk]=x/kkk;//a数组表示在ss[i]中取第几位字符。
                x-=a[kk--]*kkk;
                kkk/=k;
        }

        for(int i=0; i<m; i++)
                s[d[i]]=ss[i][a[m-i-1]];//更新字符串。
                
        puts(s+1);
}