生成字符串等于给定字符内的给定总和

时间:2018-03-14 17:46:00

标签: arrays string algorithm data-structures

给定两个数字n和k,找到一个小写字母的字符串s,使得字符串中所有元素的值之和等于k。 如果存在许多这样的字符串,请找到字典上最小的字符串。

ith小写字母的值是i,例如,a的值是1,b是2,依此类推。

输入格式: 第一行输入包含一个整数t,表示测试用例的数量。 每行包含两行以空格分隔的整数n和k。

输出格式: 打印字典最小的长度为n的字符串,其字符串和值等于k。

约束:

1< = t< = 50

1< = n< = 2 * 10 ^ 5

n< = k< = 26 * n

示例输入:

2

5 42

3 25

示例输出:

aaamz

AAW

说明:

aaamz的字符串值也是42,它是最小的词典字符串,它包含5个字符,字符串值为42。

6 个答案:

答案 0 :(得分:1)

generateLexicoString(int n, int k){
char[] a = new char[n];
    // Initially fill the array with character 'a'
    for(int i=0;i<n;i++){
        a[i] = 'a'; 
    }
    int a_count = n;
    int tot_count = k;
    int x = tot_count - a_count;
    for(int i=n-1;i>=0; i--){
        if(x> 26){
            a[i] = 'z';
            x = (x-26)+1;
        }
        else if(x > 0){
            a[i] = (char) (a[i] + x);
            x = 0;
        }
        if(x == 0)
            break;
    }
    String op="";
    for(int i=0;i<n;i++){
        op = op+a[i];
    }
    System.out.println(op);
}

答案 1 :(得分:1)

您可以使用“System.out.print(a [i]);”来避免字符串追加如下。

for(int i=0;i<n;i++){ System.out.print(a[i]); }

答案 2 :(得分:1)

抱歉,但我认为这里接受的解决方案缺少一个案例:

如果n = 2,则k = 28。 那么解决方案应该是“bz”。

 generateLexicoString(int n, int k){
char[] a = new char[n];
    // Initially fill the array with character 'a'
    for(int i=0;i<n;i++){
        a[i] = 'a'; 
    }
    int a_count = n;
    int tot_count = k;
    int x = tot_count - a_count;
    for(int i=n-1;i>=0; i--){
        if(x> 26){
            a[i] = 'z';
            x = (x-26)+1;
        }
        else if(x == 26){
            a[i] = (char) (a[i] + 25);
            x = 1;
        }
        else if(x > 0){
            a[i] = (char) (a[i] + x);
            x = 0;
        }
        if(x == 0)
            break;
    }

Char数组 a 将是我们的答案。

答案 3 :(得分:1)

在上面提供的解决方案中,它错过了x恰好是26的情况。(例如,这将失败,例如,n = 2且k = 28)。 因此,需要进行一些修改。

generateLexicoString(int n, int k){
    char[] a = new char[n];
    // Initially fill the array with character 'a'
    for(int i=0;i<n;i++){
        a[i] = 'a'; 
    }
    int a_count = n;
    int tot_count = k;
    int x = tot_count - a_count;
    for(int i=n-1;i>=0; i--){
        if(x> 26){
            a[i] = 'z';
            x = (x-26)+1;
        }
        else if(x == 26){
         a[i] = (char) (a[i] + 25);
          x = x - 25; 
        }
        else if(x > 0){
            a[i] = (char) (a[i] + x);
            x = 0;
        }
        if(x == 0)
            break;
    }
    String op= String.valueOf(a);
    System.out.println(op);
    // return op;
}

答案 4 :(得分:0)

#include<bits/stdc++.h>
using namespace std;

int main() 
{
    int t;
    cin>>t;

    while(t--)
    {
        int n,k;
        cin>>n>>k;

        string s="";

        for(int i=n;i>0;i--)
        {
            if(k - (i-1) > 26)
            {
                s = 'z' + s;
                k-=26;
            }
            else
            {
                char c = 'a' - 1 + k - (i-1);
                s =  c + s;
                k=i-1;
            }
        }

        cout<<s<<endl;
    }
    return 0;
}

答案 5 :(得分:0)

     char arr[] = new char[n];

        for (int i = n - 1; i >= 0; i--) {

           if(k>0){
               if(k>26){
                   k = k-26;
                   arr[i] = 'z';
               }else{
                   int temp = k-i;
                   arr[i] = (char)(temp + 97 - 1);
                   k -= temp;
               }
           }else{
               break;
           }
        }

        return String.valueOf(arr);
相关问题