给定一个整数K和一个数字字符串str ,任务是从给定的字符串找到可以被K整除的最长子序列。
例子:
Input: str = “121400”, K = 8
Output: 121400
Explanation:
Since the whole string is divisible by 8, the entire string is the answer.
Input: str: “7675437”, K = 64
Output: 64
Explanation:
The longest subsequence from the string which is divisible by 64, is “64” itself.
方法:这个想法是找到给定字符串的所有子序列,对于每个子序列,检查它的整数表示是否可以被K整除。请按照以下步骤解决问题:
- 遍历字符串。对于遇到的每个字符,存在两种可能性。
- 要么考虑子序列中的当前字符,要么不考虑。对于这两种情况,继续处理字符串的下一个字符并找到可被K整除的最长子序列。
- 将上面得到的最长子序列与当前最长子序列的最大长度进行比较,并进行相应的更新。
- 对字符串的每个字符重复上述步骤,最后打印得到的最大长度。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to if the integer representation
// of the current string is divisible by K
bool isdivisible(string& newstr, long long K)
{
// Stores the integer
// representation of the string
long long num = 0;
for (int i = 0; i < newstr.size(); i++) {
num = num * 10 + newstr[i] - '0';
}
// Check if the num is
// divisible by K
if (num % K == 0)
return true;
else
return false;
}
// Function to find the longest subsequence
// which is divisible by K
void findLargestNo(string& str, string& newstr,
string& ans, int index,
long long K)
{
if (index == (int)str.length()) {
// If the number is divisible by K
if (isdivisible(newstr, K)) {
// If current number is the
// maximum obtained so far
if ((ans < newstr
&& ans.length() == newstr.length())
|| newstr.length() > ans.length()) {
ans = newstr;
}
}
return;
}
string x = newstr + str[index];
// Include the digit at current index
findLargestNo(str, x, ans, index + 1, K);
// Exclude the digit at current index
findLargestNo(str, newstr, ans, index + 1, K);
}
// Driver Code
int main()
{
string str = "121400";
string ans = "", newstr = "";
long long K = 8;
findLargestNo(str, newstr, ans, 0, K);
// Printing the largest number
// which is divisible by K
if (ans != "")
cout << ans << endl;
// If no number is found
// to be divisible by K
else
cout << -1 << endl;
}
Java
// Java program for the
// above approach
import java.util.*;
import java.lang.*;
class GFG{
// Function to if the integer representation
// of the current string is divisible by K
static boolean isdivisible(StringBuilder newstr,
long K)
{
// Stores the integer
// representation of the string
long num = 0;
for(int i = 0; i < newstr.length(); i++)
{
num = num * 10 + newstr.charAt(i) - '0';
}
// Check if the num is
// divisible by K
if (num % K == 0)
return true;
else
return false;
}
// Function to find the longest
// subsequence which is divisible
// by K
static void findLargestNo(String str, StringBuilder newstr,
StringBuilder ans, int index,
long K)
{
if (index == str.length())
{
// If the number is divisible by K
if (isdivisible(newstr, K))
{
// If current number is the
// maximum obtained so far
if ((newstr.toString().compareTo(ans.toString()) > 0 &&
ans.length() == newstr.length()) ||
newstr.length() > ans.length())
{
ans.setLength(0);
ans.append(newstr);
}
}
return;
}
StringBuilder x = new StringBuilder(
newstr.toString() + str.charAt(index));
// Include the digit at current index
findLargestNo(str, x, ans, index + 1, K);
// Exclude the digit at current index
findLargestNo(str, newstr, ans, index + 1, K);
}
// Driver code
public static void main (String[] args)
{
String str = "121400";
StringBuilder ans = new StringBuilder(),
newstr = new StringBuilder();
long K = 8;
findLargestNo(str, newstr, ans, 0, K);
// Printing the largest number
// which is divisible by K
if (ans.toString() != "")
System.out.println(ans);
// If no number is found
// to be divisible by K
else
System.out.println(-1);
}
}
// This code is contributed by offbeat
C#
// C# program for the above approach
using System;
using System.Text;
class GFG{
// Function to if the integer representation
// of the current string is divisible by K
static bool isdivisible(StringBuilder newstr,
long K)
{
// Stores the integer representation
// of the string
long num = 0;
for(int i = 0; i < newstr.Length; i++)
{
num = num * 10 + newstr[i] - '0';
}
// Check if the num is
// divisible by K
if (num % K == 0)
return true;
else
return false;
}
// Function to find the longest
// subsequence which is divisible
// by K
static void findLargestNo(String str, StringBuilder newstr,
StringBuilder ans, int index,
long K)
{
if (index == str.Length)
{
// If the number is divisible by K
if (isdivisible(newstr, K))
{
// If current number is the
// maximum obtained so far
if ((newstr.ToString().CompareTo(ans.ToString()) > 0 &&
ans.Length == newstr.Length) ||
newstr.Length > ans.Length)
{
ans.EnsureCapacity(0);//.SetLength(0);
ans.Append(newstr);
}
}
return;
}
StringBuilder x = new StringBuilder(
newstr.ToString() + str[index]);
// Include the digit at current index
findLargestNo(str, x, ans, index + 1, K);
// Exclude the digit at current index
findLargestNo(str, newstr, ans, index + 1, K);
}
// Driver code
public static void Main(String[] args)
{
String str = "121400";
StringBuilder ans = new StringBuilder(),
newstr = new StringBuilder();
long K = 8;
findLargestNo(str, newstr, ans, 0, K);
// Printing the largest number
// which is divisible by K
if (ans.ToString() != "")
Console.WriteLine(ans);
// If no number is found
// to be divisible by K
else
Console.WriteLine(-1);
}
}
// This code is contributed by gauravrajput1
Javascript
输出:
121400
时间复杂度: O(N * 2 N )
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live