二进制字符串子序列中可能的最大素数
给定一个二进制字符串,任务是通过给定二进制字符串的子序列的十进制表示形式找到可能的最大素数。如果无法获得素数,则打印-1 。
例子:
Input: S = “1001”
Output: 5
Explanation: Out of all subsequences of the string “1001”, the largest prime number that can be obtained is “101” (= 5).
Input: “1011”
Output: 11
Explanation: Out of all subsequences of the string “1011”, the largest prime number that can be obtained is “1011” (= 11).
方法:为了解决这个问题,想法是生成字符串的所有可能的子序列,并将每个子序列转换为其等效的十进制形式。打印从此子序列中获得的最大素数。
请按照以下步骤解决此问题:
- 初始化一个对向量,比如vec,用于分别在Pair.first和Pair.second中存储字符串对及其等效的十进制值。
- 初始化一个变量,比如ans,以存储所需的答案。
- 从i = 0到字符串s 的长度迭代一个循环:
- 从j = 0到vec 的长度迭代一个循环:
- 将第j对存储在temp 中。
- 如果字符串s的第 i 个字符是'1 ':
- 在temp.first中添加字符。
- 通过左移当前值并将其加1来更新temp.second的值。
- 除此以外:
- 在temp.first中添加字符。
- 通过左移当前值并向其添加0来更新temp.second的值。
- 将此临时对存储到vec中。
- 如果temp.second是素数:
- 将ans和temp.second的最大值存储在ans中。
- 如果ans等于0 :
- 不能从字符串s获得素数。
- 除此以外:
- 打印答案。
- 从j = 0到vec 的长度迭代一个循环:
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
#include
using namespace std;
// Function to check if a
// number is prime or not
bool isPrime(int x)
{
if (x <= 1)
return false;
for (int i = 2; i * i <= x; i++) {
if (x % i == 0)
// Return not prime
return false;
}
// If prime return true
return true;
}
// Function to find the largest prime
// number possible from a subsequence
void largestPrime(string s)
{
// Stores pairs of subsequences and
// their respective decimal value
vector > vec{ { "", 0 } };
// Stores the answer
int ans = 0;
// Traverse the string
for (int i = 0; i < s.length(); i++) {
// Stores the size of the vector
int n = vec.size();
// Traverse the vector
for (int j = 0; j < n; j++) {
// Extract the current pair
pair temp = vec[j];
// Get the binary string from the pair
string str = temp.first;
// Stores equivalent decimal values
int val = temp.second;
// If the current character is '1'
if (s[i] == '1') {
// Add the character
// to the subsequence
temp.first = str + '1';
// Update the value by left
// shifting the current
// value and adding 1 to it
temp.second = ((val << 1) + 1);
}
// If s[i]=='0'
else {
// Add the character
// to the subsequence
temp.first = str + '0';
// Update the value by left
// shifting the current
// value and adding 0 to it
temp.second = ((val << 1) + 0);
}
// Store the subsequence in the vector
vec.push_back(temp);
// Check if the decimal
// representation of current
// subsequence is prime or not
int check = temp.second;
// If prime
if (isPrime(check)) {
// Update the answer
// with the largest one
ans = max(ans, check);
}
}
}
// If no prime number
// could be obtained
if (ans == 0)
cout << -1 << endl;
else
cout << ans << endl;
}
// Driver Code
int main()
{
// Input String
string s = "110";
largestPrime(s);
return 0;
}
Python3
# Python3 program to implement
# the above approach
# Function to check if a
# number is prime or not
def isPrime(x):
if (x <= 1):
return False
for i in range(2, x + 1):
if i * i > x:
break
if (x % i == 0):
# Return not prime
return False
# If prime return true
return True
# Function to find the largest prime
# number possible from a subsequence
def largestPrime(s):
# Stores pairs of subsequences and
# their respective decimal value
vec = [["", 0]]
# Stores the answer
ans = 0
# Traverse the string
for i in range(len(s)):
# Stores the size of the vector
n = len(vec)
# Traverse the vector
for j in range(n):
# Extract the current pair
temp = vec[j]
# Get the binary string from the pair
str = temp[0]
# Stores equivalent decimal values
val = temp[1]
# If the current character is '1'
if (s[i] == '1'):
# Add the character
# to the subsequence
temp[0] = str + '1'
# Update the value by left
# shifting the current
# value and adding 1 to it
temp[1] = ((val << 1) + 1)
# If s[i]=='0'
else:
# Add the character
# to the subsequence
temp[0] = str + '0'
# Update the value by left
# shifting the current
# value and adding 0 to it
temp[1] = ((val << 1) + 0)
# Store the subsequence in the vector
vec.append(temp)
# Check if the decimal
# representation of current
# subsequence is prime or not
check = temp[1]
# If prime
if (isPrime(check)):
# Update the answer
# with the largest one
ans = max(ans, check)
break
# If no prime number
# could be obtained
if (ans == 0):
print(-1)
else:
print(ans)
# Driver Code
if __name__ == '__main__':
# Input String
s = "110"
largestPrime(s)
# This code is contributed by mohit kumar 29
Javascript
输出:
3
时间复杂度:O(2 N * √N),其中N是字符串的长度。
辅助空间: O(2 N * N)