通过重复追加和修剪构造二元回文
给定 n 和 k,构造一个大小为 n 的回文,使用大小为 k 的二进制数重复自身以包裹回文中。回文必须始终以 1 开头,并且包含最大数量的零。
例子 :
Input : n = 5, k = 3
Output : 11011
Explanation : the 3 sized substring is
110 combined twice and trimming the extra
0 in the end to give 11011.
Input : n = 2, k = 8
Output : 11
Explanation : the 8 sized substring is 11......
wrapped to two places to give 11.
天真的方法是尝试从 1 开始的每个大小为 k 的回文,从而形成大小为 n 的回文。这种方法具有指数级的复杂性。
更好的方法是使用索引初始化 k 大小的二进制数,并以应有的方式连接回文。就像回文的最后一个字符应该匹配到第一个字符,找到将在这些位置出现的索引并将它们链接起来。将与第 0 个索引链接的每个字符设置为 1,字符串就准备好了。这种方法将具有线性复杂性。
在这种方法中,首先将 k 大小的二进制文件的索引放入数组中,例如如果 n = 7,k = 3 arr 变为 [0, 1, 2, 0, 1, 2, 0]。接着在 connectchars 图中,通过回文的第 k 个和第 (n – k – 1) 个变量应该相同的属性,连接应该相同的 k 大小二进制的索引,使得 0 链接到 1 (反之亦然),1 链接到 2(反之亦然)等等。之后,检查 connectchars 数组中与 0 链接的内容,并使用 dfs 方法将所有关联的索引设为一个(因为第一个数字应该非零)。在 dfs 中,传递 0、最终答案字符串和图形。首先使父级为 1 并检查其子级是否为零,如果它们使它们和它们的子级为 1。这仅使 k 大小字符串的所需索引为 1,其他索引为零。最后,答案包含从 0 到 k – 1 的索引,并且对应于 arr 的数字被打印出来。
C++
// CPP code to form binary palindrome
#include
#include
using namespace std;
// function to apply DFS
void dfs(int parent, int ans[], vector connectchars[])
{
// set the parent marked
ans[parent] = 1;
// if the node has not been visited set it and
// its children marked
for (int i = 0; i < connectchars[parent].size(); i++) {
if (!ans[connectchars[parent][i]])
dfs(connectchars[parent][i], ans, connectchars);
}
}
void printBinaryPalindrome(int n, int k)
{
int arr[n], ans[n] = { 0 };
// link which digits must be equal
vector connectchars[k];
for (int i = 0; i < n; i++)
arr[i] = i % k;
// connect the two indices
for (int i = 0; i < n / 2; i++) {
connectchars[arr[i]].push_back(arr[n - i - 1]);
connectchars[arr[n - i - 1]].push_back(arr[i]);
}
// set everything connected to
// first character as 1
dfs(0, ans, connectchars);
for (int i = 0; i < n; i++)
cout << ans[arr[i]];
}
// driver code
int main()
{
int n = 10, k = 4;
printBinaryPalindrome(n, k);
return 0;
}
Java
// JAVA code to form binary palindrome
import java.util.*;
class GFG
{
// function to apply DFS
static void dfs(int parent, int ans[],
Vector connectchars[])
{
// set the parent marked
ans[parent] = 1;
// if the node has not been visited set it and
// its children marked
for (int i = 0; i < connectchars[parent].size(); i++)
{
if (ans[connectchars[parent].get(i)] != 1)
dfs(connectchars[parent].get(i), ans, connectchars);
}
}
static void printBinaryPalindrome(int n, int k)
{
int []arr = new int[n];
int []ans = new int[n];
// link which digits must be equal
Vector []connectchars = new Vector[k];
for (int i = 0; i < k; i++)
connectchars[i] = new Vector();
for (int i = 0; i < n; i++)
arr[i] = i % k;
// connect the two indices
for (int i = 0; i < n / 2; i++)
{
connectchars[arr[i]].add(arr[n - i - 1]);
connectchars[arr[n - i - 1]].add(arr[i]);
}
// set everything connected to
// first character as 1
dfs(0, ans, connectchars);
for (int i = 0; i < n; i++)
System.out.print(ans[arr[i]]);
}
// Driver code
public static void main(String[] args)
{
int n = 10, k = 4;
printBinaryPalindrome(n, k);
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 code to form binary palindrome
# function to apply DFS
def dfs(parent, ans, connectchars):
# set the parent marked
ans[parent] = 1
# if the node has not been visited
# set it and its children marked
for i in range(len(connectchars[parent])):
if (not ans[connectchars[parent][i]]):
dfs(connectchars[parent][i], ans,
connectchars)
def printBinaryPalindrome(n, k):
arr = [0] * n
ans = [0] * n
# link which digits must be equal
connectchars = [[] for i in range(k)]
for i in range(n):
arr[i] = i % k
# connect the two indices
for i in range(int(n / 2)):
connectchars[arr[i]].append(arr[n - i - 1])
connectchars[arr[n - i - 1]].append(arr[i])
# set everything connected to
# first character as 1
dfs(0, ans, connectchars)
for i in range(n):
print(ans[arr[i]], end = "")
# Driver Code
if __name__ == '__main__':
n = 10
k = 4
printBinaryPalindrome(n, k)
# This code is contributed by PranchalK
C#
// C# code to form binary palindrome
using System;
using System.Collections.Generic;
class GFG
{
// function to apply DFS
static void dfs(int parent, int []ans,
List []connectchars)
{
// set the parent marked
ans[parent] = 1;
// if the node has not been visited set it and
// its children marked
for (int i = 0; i < connectchars[parent].Count; i++)
{
if (ans[connectchars[parent][i]] != 1)
dfs(connectchars[parent][i], ans, connectchars);
}
}
static void printBinaryPalindrome(int n, int k)
{
int []arr = new int[n];
int []ans = new int[n];
// link which digits must be equal
List []connectchars = new List[k];
for (int i = 0; i < k; i++)
connectchars[i] = new List();
for (int i = 0; i < n; i++)
arr[i] = i % k;
// connect the two indices
for (int i = 0; i < n / 2; i++)
{
connectchars[arr[i]].Add(arr[n - i - 1]);
connectchars[arr[n - i - 1]].Add(arr[i]);
}
// set everything connected to
// first character as 1
dfs(0, ans, connectchars);
for (int i = 0; i < n; i++)
Console.Write(ans[arr[i]]);
}
// Driver code
public static void Main(String[] args)
{
int n = 10, k = 4;
printBinaryPalindrome(n, k);
}
}
// This code is contributed by PrinciRaj1992
Javascript
1100110011
时间复杂度:O(n)