在 Thue-Morse 序列的第 N 项中查找前 K 个字符
给定两个整数N和K ,任务是打印Thue -Morse 序列的第 N 项的前K位。 Thue-Morse 序列是一个二进制序列。它以“0”作为第一项。然后通过将“0”替换为“01”和“1”替换为“10”来生成下一项。
例子:
Input: N = 3, K = 2
Output: 01
Explanation: The 1st term is “0”.
The 2nd term is obtained by replacing “0” with “01” i.e. 2nd term is “01”.
The 3rd term in the sequence is obtained by replacing “0” with “01” and “1” with “10”.
So the 3rd term becomes “0110”. Hence, the 1st 2 characters of the 3rd term is “01”.
Input: N = 4, K = 7
Output: 0110100
方法:解决这个问题的基本方法是生成序列的第 N项并打印该项的前K个字符。这可以使用这里讨论的算法来完成。
时间复杂度:O(N * 2 N )
辅助空间:O(1)
有效方法:上述方法可以通过观察第 i项是第 (i – 1) 项的串联和第 ( i – 1)项的倒数来优化,其中倒数意味着改变二进制整数中所有位的极性.因此,第 x项, A i [x] = A i-1 [x – 1] if ( x < 2 i-1 ),否则A i [x] = !A i-1 [x – 2 i-1 ] .因此,使用这种关系,可以创建一个递归函数来计算第N项中每个位的值。
下面是上述方法的实现:
C++
#include
using namespace std;
// Recursive function to find the
// value of the kth bit in Nth term
int findDig(int N, long K, int curr)
{
// Base Case
if (N == 0) {
return curr;
}
// Stores the middle index
long middle = (long)pow(2, N) / 2;
// If K lies in 1st part
if (K <= middle) {
// Recursive Call
return findDig(N - 1, K, curr);
}
// If K lies in 2nd part
// having inverted value
else {
if (curr == 0) {
curr = 1;
}
else {
curr = 0;
}
// Recursive Call
return findDig(N - 1,
K - middle, curr);
}
}
// Function to find first K characters
// in Nth term of Thue-Morse sequence
void firstKTerms(int N, int K)
{
// Loop to iterate all K bits
for (int i = 1; i <= K; ++i)
{
// Print value of ith bit
cout << (findDig(N, i, 0));
}
}
// Driver Code
int main() {
int N = 4;
int K = 7;
firstKTerms(N, K);
return 0;
}
// This code is contributed by hrithikgarg03188.
Java
// Java Implementation of the above approach
import java.io.*;
import java.util.*;
class GFG {
// Recursive function to find the
// value of the kth bit in Nth term
public static int findDig(int N, long K,
int curr)
{
// Base Case
if (N == 0) {
return curr;
}
// Stores the middle index
long middle = (long)Math.pow(2, N) / 2;
// If K lies in 1st part
if (K <= middle) {
// Recursive Call
return findDig(N - 1, K, curr);
}
// If K lies in 2nd part
// having inverted value
else {
if (curr == 0) {
curr = 1;
}
else {
curr = 0;
}
// Recursive Call
return findDig(N - 1,
K - middle, curr);
}
}
// Function to find first K characters
// in Nth term of Thue-Morse sequence
public static void firstKTerms(int N, int K)
{
// Loop to iterate all K bits
for (int i = 1; i <= K; ++i) {
// Print value of ith bit
System.out.print(findDig(N, i, 0));
}
}
// Driver Code
public static void main(String args[])
{
int N = 4;
int K = 7;
firstKTerms(N, K);
}
}
Python3
# Recursive function to find the
# value of the kth bit in Nth term
def findDig(N, K, curr):
# Base Case
if (N == 0):
return curr
# Stores the middle index
middle = pow(2, N) // 2
# If K lies in 1st part
if (K <= middle):
# Recursive Call
return findDig(N - 1, K, curr)
# If K lies in 2nd part
# having inverted value
else:
if (curr == 0):
curr = 1
else:
curr = 0
# Recursive Call
return findDig(N - 1, K - middle, curr)
# Function to find first K characters
# in Nth term of Thue-Morse sequence
def firstKTerms(N, K):
# Loop to iterate all K bits
for i in range(1, K+1):
# Print value of ith bit
print(findDig(N, i, 0), end="")
# Driver Code
if __name__ == "__main__":
N = 4
K = 7
firstKTerms(N, K)
# This code is contributed by rakeshsahni
C#
// C# Implementation of the above approach
using System;
class GFG
{
// Recursive function to find the
// value of the kth bit in Nth term
public static int findDig(int N, long K, int curr)
{
// Base Case
if (N == 0)
{
return curr;
}
// Stores the middle index
long middle = (long)Math.Pow(2, N) / 2;
// If K lies in 1st part
if (K <= middle)
{
// Recursive Call
return findDig(N - 1, K, curr);
}
// If K lies in 2nd part
// having inverted value
else
{
if (curr == 0)
{
curr = 1;
}
else
{
curr = 0;
}
// Recursive Call
return findDig(N - 1,
K - middle, curr);
}
}
// Function to find first K characters
// in Nth term of Thue-Morse sequence
public static void firstKTerms(int N, int K)
{
// Loop to iterate all K bits
for (int i = 1; i <= K; ++i)
{
// Print value of ith bit
Console.Write(findDig(N, i, 0));
}
}
// Driver Code
public static void Main()
{
int N = 4;
int K = 7;
firstKTerms(N, K);
}
}
// This code is contributed by saurabh_jaiswal.
Javascript
0110100
时间复杂度:O(K*log N)
辅助空间:O(1)