给定正整数n ,任务是在按照字典顺序(词典)排序的两个符号a和b上,在所有可能的字符串的以下无限列表中找到第n个字符串。
a, b, aa, ab, ba, bb, aaa, aab, aba, abb, baa, bab, bba, bbb, aaaa, …
例子:
Input: n = 6
Output: bb
Input: n = 11
Output: baa
一种简单的方法是生成直到n的所有字符串,然后确定第n个字符串。但是,该方法不适用于较大的n值。
一种有效的方法基于以下事实:可以使用2个符号生成的长度为k的字符串数为2 k 。基于此,我们可以根据实际索引(n)相对于列表中字符串的长度来计算相对索引。然后,在对列表进行排序时,可以使用相对索引的二进制形式轻松确定第n个索引处的字符串。以下公式用于计算,
relative index = n + 1 – 2floor(log(n + 1))
考虑以下示例:
Let n = 11 then floor(log(n + 1)) = 3.
This suggests that index n consists of a length 3 string and length 3 strings start form (23 – 1) = 7th index and 7th index contains the string “aaa”.
Therefore, relative index = 11 + 1 – 23 = 4.
This is the index relative to 7. Now, the string at index n = 11 can be simply obtained from the binary interpretation of the relative index 4.
Here 0 means a and 1 means b. The table below illustrates this:
Relative Index | Binary | String |
---|---|---|
0 | 000 | aaa |
1 | 001 | aab |
2 | 010 | aba |
3 | 011 | abb |
4 | 100 | baa |
5 | 101 | bab |
6 | 110 | bba |
7 | 111 | bbb |
因此,位于第11个索引(相对索引4 )的字符串为“ baa”
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
#define ll long long int
// Function to return the nth string in the required sequence
string obtain_str(ll n)
{
// Length of the resultant string
ll len = (int)log2(n + 1);
// Relative index
ll rel_ind = n + 1 - pow(2, len);
ll i = 0;
string str = "";
for (i = 0; i < len; i++) {
// Initial string of length len consists of
// all a's since the list is sorted
str += 'a';
}
i = 0;
// Convert relative index to Binary form and set
// 0 = a and 1 = b
while (rel_ind > 0) {
if (rel_ind % 2 == 1)
str[i] = 'b';
rel_ind /= 2;
i++;
}
// Reverse and return the string
reverse(str.begin(), str.end());
return str;
}
// Driver function
int main()
{
ll n = 11;
cout << obtain_str(n);
return 0;
}
Java
// Java Implementation of the above approach
import java.io.*;
import java.util.*;
class Gfg {
// Function to return the nth string in the required sequence
static String obtain_str(int n)
{
// Length of the resultant string
int len = (int)Math.floor((Math.log(n + 1) / Math.log(2)));
// Relative Index
int rel_ind = n + 1 - (int)Math.pow(2, len);
int i = 0;
StringBuilder str = new StringBuilder();
for (i = 0; i < len; i++) {
// Initial string of length len consists of
// all a's since the list is sorted
str.append('a');
}
i = 0;
// Convert relative index to Binary form and set
// 0 = a and 1 = b
while (rel_ind > 0) {
if (rel_ind % 2 == 1)
str.setCharAt(i, 'b');
rel_ind /= 2;
i++;
}
// Reverse and return the string
str = str.reverse();
return str.toString();
}
// Driver funtion
public static void main(String args[])
{
int n = 11;
System.out.print(obtain_str(n));
}
}
Python3
# Python3 implementation of the
# above approach
# from math lib import log2 function
from math import log2
# Function to return the nth string
# in the required sequence
def obtain_str(n) :
# Length of the resultant string
length = int(log2(n + 1))
# Relative index
rel_ind = n + 1 - pow(2, length)
i = 0
string = ""
for i in range(length) :
# Initial string of length len consists
# of all a's since the list is sorted
string += 'a'
i = 0
string_list = list(string)
# Convert relative index to Binary
# form and set 0 = a and 1 = b
while (rel_ind > 0) :
if (rel_ind % 2 == 1) :
string_list[i] = 'b'
rel_ind //= 2
i += 1
# Reverse and return the string
string_list.reverse()
string = "".join(string_list)
return string
# Driver Code
if __name__ == "__main__" :
n = 11
print(obtain_str(n))
# This code is contributed by Ryuga
C#
// C# Implementation of the above approach
using System;
using System.Text;
class GFG
{
// Function to return the nth string
// in the required sequence
static String obtain_str(int n)
{
// Length of the resultant string
int len = (int)Math.Floor((Math.Log(n + 1) /
Math.Log(2)));
// Relative Index
int rel_ind = n + 1 - (int)Math.Pow(2, len);
int i = 0;
StringBuilder str = new StringBuilder();
for (i = 0; i < len; i++)
{
// Initial string of length len consists of
// all a's since the list is sorted
str.Append('a');
}
i = 0;
// Convert relative index to Binary form and set
// 0 = a and 1 = b
while (rel_ind > 0)
{
if (rel_ind % 2 == 1)
str[i]='b';
rel_ind /= 2;
i++;
}
// Reverse and return the string
return reverse(str.ToString());
}
static String reverse(String input)
{
char[] a = input.ToCharArray();
int l, r = a.Length - 1;
for (l = 0; l < r; l++, r--)
{
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.Join("", a);
}
// Driver funtion
public static void Main(String []args)
{
int n = 11;
Console.Write(obtain_str(n));
}
}
// This code is contributed by PrinciRaj1992
PHP
0)
{
if ($rel_ind % 2 == 1)
$str[$i] = 'b';
$rel_ind = (int)($rel_ind / 2);
$i++;
}
// Reverse and return the string
return strrev($str);
}
// Driver Code
$n = 11;
echo obtain_str($n);
// This code is contributed
// by chandan_jnu
?>
baa