Thue-Morse序列,或Prouhet-Thue-Morse序列,是一个0s和1s的无限二进制序列。该序列是通过以0开头并相继附加到目前为止获得的序列的布尔补码来获得的。
前几个步骤:
Start with 0
Append complement of 0, we get 01
Append complement of 01, we get 0110
Append complement of 0110, we get 01101001
给定整数n 。任务是找到由Thue–Morse序列形成的第n个字符串,即Thue–Morse序列的长度2 n-1的前缀。
例子:
Input : n = 4
Output : 01101001
We get 0, 01, 0110 and 01101001
in fourth iteration.
Input : n = 3
Output : 0110
这个想法是用0初始化输出字符串,然后运行一次n – 1次循环,并为每次迭代找到字符串的补码并将其附加到字符串。
以下是此方法的实现:
C++
// CPP Program to find nth term of Thue-Morse sequence.
#include
using namespace std;
// Return the complement of the binary string.
string complement(string s)
{
string comps;
// finding the complement of the string.
for (int i = 0; i < s.length(); i++) {
// if character is 0, append 1
if (s.at(i) == '0')
comps += '1';
// if character is 1, append 0.
else
comps += '0';
}
return comps;
}
// Return the nth term of Thue-Morse sequence.
string nthTerm(int n)
{
// Initialing the string to 0
string s = "0";
// Running the loop for n - 1 time.
for (int i = 1; i < n; i++)
// appending the complement of
// the string to the string.
s += complement(s);
return s;
}
// Driven Program
int main()
{
int n = 4;
cout << nthTerm(n) << endl;
return 0;
}
Java
// Java Program to find nth
// term of Thue-Morse sequence.
class GFG
{
// Return the complement
// of the binary String.
static String complement(String s)
{
String comps = "";
// finding the complement
// of the String.
for (int i = 0; i < s.length(); i++)
{
// if character is 0,
// append 1
if (s.charAt(i) == '0')
comps += '1';
// if character is 1,
// append 0.
else
comps += '0';
}
return comps;
}
// Return the nth term
// of Thue-Morse sequence.
static String nthTerm(int n)
{
// Initialing the
// String to 0
String s = "0";
// Running the loop
// for n - 1 time.
for (int i = 1; i < n; i++)
// appending the complement of
// the String to the String.
s += complement(s);
return s;
}
// Driven Code
public static void main(String[] args)
{
int n = 4;
System.out.print(nthTerm(n));
}
}
// This code is contributed by
// mits
Python3
# Python3 Program to find nth term of
# Thue-Morse sequence.
# Return the complement of the
# binary string.
def complement(s):
comps = "";
# finding the complement
# of the string.
for i in range(len(s)):
# if character is 0, append 1
if (s[i] == '0'):
comps += '1';
# if character is 1, append 0.
else:
comps += '0';
return comps;
# Return the nth term of
# Thue-Morse sequence.
def nthTerm(n):
# Initialing the string to 0
s = "0";
# Running the loop for n - 1 time.
for i in range(1, n):
# appending the complement of
# the string to the string.
s += complement(s);
return s;
# Driver Code
n = 4;
print(nthTerm(n));
# This code is contributed
# by mits
C#
// C# Program to find nth
// term of Thue-Morse sequence.
using System;
class GFG
{
// Return the complement
// of the binary string.
static string complement(string s)
{
string comps = "";
// finding the complement
// of the string.
for (int i = 0; i < s.Length; i++)
{
// if character is 0,
// append 1
if (s[i] == '0')
comps += '1';
// if character is 1,
// append 0.
else
comps += '0';
}
return comps;
}
// Return the nth term
// of Thue-Morse sequence.
static string nthTerm(int n)
{
// Initialing the
// string to 0
string s = "0";
// Running the loop
// for n - 1 time.
for (int i = 1; i < n; i++)
// appending the complement of
// the string to the string.
s += complement(s);
return s;
}
// Driven Code
static void Main()
{
int n = 4;
Console.Write(nthTerm(n));
}
}
// This code is contributed by
// Manish Shaw(manishshaw1)
PHP
C++
#include
using namespace std;
int main() {
cout<<"GFG!";
return 0;
}
Javascript
输出:
01101001