String 给定两个字符串,找到最长的常用字符包
这个问题与从两个字符串中查找最长序列或子字符串略有不同 给定两个大小相同的字符串N,从每个字符串中找出最长的子字符串,以便子字符串包含相同的字符包 这两个子字符串不一定具有相同的序列。但他们必须有相同的一袋chars 比如说, a=ABCDDEGF b=FPCDBDAX 最长的匹配袋是ABCDD(ABCDD来自a,CDBDA来自b) 如何解决这个问题String 给定两个字符串,找到最长的常用字符包,string,algorithm,String,Algorithm,这个问题与从两个字符串中查找最长序列或子字符串略有不同 给定两个大小相同的字符串N,从每个字符串中找出最长的子字符串,以便子字符串包含相同的字符包 这两个子字符串不一定具有相同的序列。但他们必须有相同的一袋chars 比如说, a=ABCDDEGF b=FPCDBDAX 最长的匹配袋是ABCDD(ABCDD来自a,CDBDA来自b) 如何解决这个问题 更新 目标是从每个输入字符串中找到子字符串,以便它们具有相同的字符包。通过说“substring”,它们必须是连续字符 更新:最初我想到了一种
更新 目标是从每个输入字符串中找到子字符串,以便它们具有相同的字符包。通过说“substring”,它们必须是连续字符
更新:最初我想到了一种动态规划方法。它的工作原理如下 要比较两袋相同长度K的焦炭,需要O(K)时间才能达到这一目的。将每个字符串转换为缩写形式:
ABCDDEGF -> A1B1C1D2E1G1F1
FPCDBDAX -> A1B1C1D2F1P1X1
缩写形式是按字母排序,然后是字符串中的频率数。
构造、排序和比较缩短的表单总共需要O(K)个时间。(不过可以通过使用字符数组来实现)
如果两袋字符的缩短形式具有相同的字符和各自的频率,则两袋字符是相等的
此外,查找两个字符串之间的差异字符需要O(logK)时间
现在,对于两个输入字符串:
最坏的情况是O(N3),最好的情况是O(N)。有更好的主意吗?创建一组出现在
a
中的字符,以及另一组出现在b
中的字符。遍历每个字符串并从另一个字符串中删除(例如,用一些不可能的值覆盖)集合中不包含的所有字符。查找每个字符串中剩余的最长字符串(即仅包含“unstruck”字符的最长字符串)
编辑:这里有一个解决方案,大致上和上面提到的一样,但是以一种特定语言的方式(使用C++语言环境/方面):
#包括
#包括
#包括
#包括
#包括
#包括
结构过滤器:std::ctype{
过滤器(std::string const&a):std::ctype(表,false){
std::fill_n(表,std::ctype::表大小,std::ctype_基::空格);
用于(尺寸i=0;i>temp)
如果(临时大小()>最长大小())
最长=温度;
删除过滤器;
返回时间最长;
}
int main(){
std::string a=“ABCDDEGF”,b=“FPCDBDAX”;
std::cout这是我的一个相当反python的实现,它利用了python出色的内置集和字符串
a = 'ABCDDEGF'
b = 'FPCDBDAX'
best_solution = None
best_solution_total_length = 0
def try_expand(a, b, a_loc, b_loc):
# out of range checks
if a_loc[0] < 0 or b_loc[0] < 0:
return
if a_loc[1] == len(a) or b_loc[1] == len(b):
return
if set(a[a_loc[0] : a_loc[1]]) == set(b[b_loc[0] : b_loc[1]]):
global best_solution_total_length, best_solution
#is this solution better than anything before it?
if (len(a[a_loc[0] : a_loc[1]]) + len(b[b_loc[0] : b_loc[1]])) > best_solution_total_length:
best_solution = (a_loc, b_loc)
best_solution_total_length = len(a[a_loc[0] : a_loc[1]]) + len(b[b_loc[0] : b_loc[1]])
try_expand(a, b, (a_loc[0]-1, a_loc[1]), (b_loc[0], b_loc[1]))
try_expand(a, b, (a_loc[0], a_loc[1]+1), (b_loc[0], b_loc[1]))
try_expand(a, b, (a_loc[0], a_loc[1]), (b_loc[0]-1, b_loc[1]))
try_expand(a, b, (a_loc[0], a_loc[1]), (b_loc[0], b_loc[1]+1))
for a_i in range(len(a)):
for b_i in range(len(b)):
# starts of the recursive expansion from identical letters in two substrings
if a[a_i] == b[b_i]:
# if substrings were expanded from this range before then there won't be an answer there
if best_solution == None or best_solution[0][0] > a_i or best_solution[0][1] <= a_i or best_solution[1][0] > b_i or best_solution[1][1] <= b_i:
try_expand(a, b, (a_i, a_i), (b_i, b_i))
print a[best_solution[0][0] : best_solution[0][1]], b[best_solution[1][0] : best_solution[1][1]]
a='ABCDDEGF'
b='FPCDBDAX'
最佳解决方案=无
最佳解决方案总长度=0
def try_expand(a、b、a_loc、b_loc):
#超出范围检查
如果a_loc[0]<0或b_loc[0]<0:
返回
如果a_loc[1]==len(a)或b_loc[1]==len(b):
返回
如果设置(a[a_-loc[0]:a_-loc[1]])==设置(b[b_-loc[0]:b_-loc[1]]:
全球最佳解决方案总长度,最佳解决方案
#这个解决方案比以前的任何解决方案都好吗?
如果(len(a[a_-loc[0]:a_-loc[1]])+len(b[b_-loc[0]:b_-loc[1]])>最佳解决方案总长度:
最佳解决方案=(a\U loc,b\U loc)
最佳解决方案总长度=len(a[a_-loc[0]:a_-loc[1])+len(b[b_-loc[0]:b_-loc[1])
尝试扩展(a,b,(a_loc[0]-1,a_loc[1]),(b_loc[0],b_loc[1]))
尝试扩展(a,b,(a_loc[0],a_loc[1]+1),(b_loc[0],b_loc[1]))
尝试扩展(a,b,(a_loc[0],a_loc[1]),(b_loc[0]-1,b_loc[1]))
尝试扩展(a,b,(a_loc[0],a_loc[1]),(b_loc[0],b_loc[1]+1))
对于范围内的a_i(len(a)):
对于范围(len(b))内的b_i:
#从两个子字符串中的相同字母开始递归展开
如果a[a_i]==b[b_i]:
#如果子字符串在此之前已从该范围展开,则不会有答案
如果best_solution==None或best_solution[0][0]>a_i或best_solution[0][1]b_i或best_solution[1][1]请注意,这个问题不允许“贪婪”一种解决方案,通过将现有的可行袋一次扩展一个元素来构造连续较大的袋。其原因是,即使存在长度为k的可行袋,也不需要任何长度为(k-1)的可行袋,如下反例所示:
ABCD
CDAB
显然有一个长度为4的包(a:1、B:1、C:1、D:1
)由两个字符串共享,但没有共享长度为3的包。这对我来说可能是一个很难解决的问题。让我们这样来看看这个问题。这个解决方案将更加优化,并且非常容易编码,但是要通读def,您必须阅读代码才能获得想法。否则它听起来会很简单疯狂而复杂
想想这个
在您的问题中,您给出的两个示例字符串让我们将它们作为两个字符集,即{x,y,z}
以及..和..生成的子字符串(集合)将是两个字符串(集合)中共有个字符的子字符串,并且将是连续条目,并且符合条件的子字符串(ser)将是条目数最多的子字符串
以上是结果的一些属性,但仅在通过以下算法\methodolgy使用时有效
我们有两套
a={BAHYJIKLO}
b={YTSHYJLOP}
接受
aUb={-,-
ABCD
CDAB
char a[20], b[20]; //a[20] & b[30] are two strings
cin>>a; cin>>b;
int t=0;
open a temporary text file "file1" to write '(built-in-function works here)'
//a U b
for(int x=0; x<length(a); x++)
{
t=0;
for(int y=0; y<length(b); x++)
{ if( a[x] == b[y]) t=1; }
if(t == 1)
{
write 'a[x]' to the file1 '(built-in-function works here)'
t=0;
}
else
write a 'space' to the file1 '(built-in-function works here)'
}
//b U a
for(int x=0; x<length(a); x++)
{
t=0;
for(int y=0; y<length(b); x++)
{ if( b[x] == a[y]) t=1; }
if(t == 1)
{
write 'a[x]' to the file1 '(built-in-function works here)'
t=0;
}
else
write a 'space' to the file1 '(built-in-function works here)'
}
/*output in the file wil be like this
_____FILE1.txt_____
HYJ LO Y HYJLO
*/
//load all words in an array of stings from file '(built-in-function works here)'
char *words[]={"HYJ","LO","Y","HYJLO"};
int size=0,index=0;
for( int x=0; x<length(words); x++)
for( int y=0; x<length(words); y++)
{
if( x!=y && words[x] is a substring of words[y] ) // '(built-in-function works here)'
{
if( length(words[x] ) < size )
{
size = length(words[x];
index = x;
}
}
}
cout<< words[x];
//its the desired result.. its pretty old school bu i think you get the idea
}