Arrays 使用单个字符串的子序列
给定一个长度为N的字符串S。选择一个整数K和该字符串的两个非空字符子序列a和B,每个字符的长度为K,以便:Arrays 使用单个字符串的子序列,arrays,string,dynamic-programming,Arrays,String,Dynamic Programming,给定一个长度为N的字符串S。选择一个整数K和该字符串的两个非空字符子序列a和B,每个字符的长度为K,以便: A=B,即对于每个有效i,A中的第i个字符与 B中的第i个字符 让我们表示用于构造by的字符索引 a1,a2,…,aK,即A=(Sa1,Sa2,…,SaK)。类似地,让我们表示 用于通过b1、b2、…、bK构造B的字符索引 如果我们用 M、 那么M+1≤K K的最大值是多少,这样就有可能找到满足上述条件的序列A和序列B 请给出这个问题的最简单的解决方案。我无法继续讨论如何解决这个问题。
- A=B,即对于每个有效i,A中的第i个字符与 B中的第i个字符
- 让我们表示用于构造by的字符索引 a1,a2,…,aK,即A=(Sa1,Sa2,…,SaK)。类似地,让我们表示 用于通过b1、b2、…、bK构造B的字符索引
- 如果我们用 M、 那么M+1≤K
请给出这个问题的最简单的解决方案。我无法继续讨论如何解决这个问题。这个问题似乎找到了最大长度的重复子序列。 比如,如果字符串是“ababdb”,那么答案将是4。 因为abab可以生成两次,并且至少有一个索引是不同的。 所以方法是 1.生成所有可能的子序列。 2.存储并与上一个匹配。 3.如果发现任何重复项,我们将更新最大值
#include <iostream>
#include <vector>
#include<string>
using namespace std;
vector<string> test;
vector<string>::iterator iter;
int max = 0;
void Sub(string input , string output)
{
if (input.length() == 0)
{
if (output.length() > max)// store only next greater max
{
iter = std::find(test.begin(), test.end(), output);
if (iter != test.end())
{
max = output.length();
return;
}
}
else
{
return;
}
test.push_back(output);
return;
}
Sub(input.substr(1), output);
Sub(input.substr(1), output + input[0]);
}
int main()
{
string input = "ababdb";
string output = "";
Sub(input, output);
cout << max;
return 0;
}
#包括
#包括
#包括
使用名称空间std;
病媒试验;
向量:迭代器iter;
int max=0;
void子(字符串输入、字符串输出)
{
if(input.length()==0)
{
if(output.length()>max)//只存储下一个更大的max
{
iter=std::find(test.begin(),test.end(),output);
if(iter!=test.end())
{
max=output.length();
返回;
}
}
其他的
{
返回;
}
测试。推回(输出);
返回;
}
Sub(输入substr(1),输出);
Sub(输入。substr(1),输出+输入[0]);
}
int main()
{
字符串输入=“ababdb”;
字符串输出=”;
子(输入、输出);
您的ans将是这样的。。。
找到重复字符和ans之间的最小距离将是字符串总长度-距离
例如“ababdbdhfdksl”
重复字符之间的最小距离=1(中间2 b)
所以ans=长度(14)-1=13
如果所有字符都是不同的,答案将是0。其中的M是什么?Cmon man这是一个正在进行的codechef竞赛问题