Decreasing String

来源:互联网 发布:ajax获取json数据实例 编辑:程序博客网 时间:2024/06/05 22:59

Problem F: Decreasing String

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 81  Solved: 28
[Submit][Status][Web Board]

Description

You need to find a string which has exactly K positions in it such that the character at that position comes alphabetically later than the character immediately after it. If there are many such strings, print the one which has the shortest length. If there is still a tie, print the string which comes the lexicographically earliest (would occur earlier in a dictionary).

Input

The first line contains the number of test cases T. Each test case contains an integer K (≤ 100).

Output

Output T lines, one for each test case, containing the required string. Use only lower-case letters a-z.

Sample Input

212

Sample Output

bacba

HINT

其实也就是一个水坑题,不太好理解,尝试了几次才ac,

这样理解吧:

逆序输出k+1(准确的讲不是k+1)个字母,当k=1时输出ba,k=2时cba,k=3时dcba........,k=25时zyx.....cba,k=26时 bazyx........cba,k=26时 cbazyx.....cba

也就是一点,当k%26==0时输出的是ba不是a,其他的逆序按照字典序输出就行。

#include<cstdio>#include<cstring>#include<cctype>#include<algorithm>#include<set>#include<cstring>#include<string>#include<iostream>#include<cmath>#include<map>#include<vector>#include<stack>using namespace std;char b[105];int main(){    int t;    scanf("%d",&t);    while(t--){            memset(b,0,sizeof(b));        int n;        scanf("%d",&n);        char a='a';        for(int i=0;i<=n;i++){//我直接用数组存,然后逆序输出            b[i]=a+i%26;            if(i%26==0&&i!=0){//满足条件的时候多加'b'到数组里面,构成ab                b[++i]='b';                n+=1;            }        }          for(int i=strlen(b)-1;i>=0;i--)            printf("%c",b[i]);             printf("\n");    }    return 0;}