C# 如何查找字符串中连续字符的交点?
假设我有以下字符串:C# 如何查找字符串中连续字符的交点?,c#,string,C#,String,假设我有以下字符串: blahFOOblahblah blahbalahbarblah FIZZblahblahblah 现在,我想询问其中的每一个,以发现其中哪些包含以下子字符串: FIZZbuzz 显然,这个字符串与#3共享单词“FIZZ” 我已经看过了,它并没有完全完成我想要的,因为它只关注字符(以任何顺序)而不是子字符串 有一些快速但相当复杂的算法通过构建和使用来解决任务。对于恒定大小的字母表,它们有O(n)时间,在最坏的情况下,它们有O(n log(n))时间,其中n是字符串的最大长度
blahFOOblahblah
blahbalahbarblah
FIZZblahblahblah
FIZZbuzz
显然,这个字符串与#3共享单词“FIZZ”
我已经看过了,它并没有完全完成我想要的,因为它只关注字符(以任何顺序)而不是子字符串 有一些快速但相当复杂的算法通过构建和使用来解决任务。对于恒定大小的字母表,它们有
O(n)
时间,在最坏的情况下,它们有O(n log(n))
时间,其中n
是字符串的最大长度
下面是一个可能的C#实现(来自)。这不是最优的,但在我们的情况下可能足够了
public int LongestCommonSubstring(string str1, string str2, out string sequence)
{
sequence = string.Empty;
if (String.IsNullOrEmpty(str1) || String.IsNullOrEmpty(str2))
return 0;
int[,] num = new int[str1.Length, str2.Length];
int maxlen = 0;
int lastSubsBegin = 0;
StringBuilder sequenceBuilder = new StringBuilder();
for (int i = 0; i < str1.Length; i++)
{
for (int j = 0; j < str2.Length; j++)
{
if (str1[i] != str2[j])
num[i, j] = 0;
else
{
if ((i == 0) || (j == 0))
num[i, j] = 1;
else
num[i, j] = 1 + num[i - 1, j - 1];
if (num[i, j] > maxlen)
{
maxlen = num[i, j];
int thisSubsBegin = i - num[i, j] + 1;
if (lastSubsBegin == thisSubsBegin)
{//if the current LCS is the same as the last time this block ran
sequenceBuilder.Append(str1[i]);
}
else //this block resets the string builder if a different LCS is found
{
lastSubsBegin = thisSubsBegin;
sequenceBuilder.Length = 0; //clear it
sequenceBuilder.Append(str1.Substring(lastSubsBegin, (i + 1) - lastSubsBegin));
}
}
}
}
}
sequence = sequenceBuilder.ToString();
return maxlen;
}
public int LongestCommonSubstring(字符串str1、字符串str2、out字符串序列)
{
sequence=string.Empty;
if(String.IsNullOrEmpty(str1)| | String.IsNullOrEmpty(str2))
返回0;
int[,]num=新int[str1.Length,str2.Length];
int maxlen=0;
int=0;
StringBuilder sequenceBuilder=新的StringBuilder();
for(int i=0;imaxlen)
{
maxlen=num[i,j];
int=i-num[i,j]+1;
如果(lastSubsBegin==thisSubsBegin)
{//如果当前LCS与上次运行此块时相同
sequenceBuilder.Append(str1[i]);
}
else//如果找到不同的LCS,此块将重置字符串生成器
{
lastSubsBegin=此SUBSBEGIN;
sequenceBuilder.Length=0;//清除它
sequenceBuilder.Append(str1.Substring(lastsubsbeing,(i+1)-lastsubsbeing));
}
}
}
}
}
sequence=sequenceBuilder.ToString();
返回maxlen;
}
您正在寻找类似的产品吗
有一些快速但相当复杂的算法通过构建和使用来解决任务。对于恒定大小的字母表,它们有O(n)
时间,在最坏的情况下,它们有O(n log(n))
时间,其中n
是字符串的最大长度
下面是一个可能的C#实现(来自)。这不是最优的,但在我们的情况下可能足够了
public int LongestCommonSubstring(string str1, string str2, out string sequence)
{
sequence = string.Empty;
if (String.IsNullOrEmpty(str1) || String.IsNullOrEmpty(str2))
return 0;
int[,] num = new int[str1.Length, str2.Length];
int maxlen = 0;
int lastSubsBegin = 0;
StringBuilder sequenceBuilder = new StringBuilder();
for (int i = 0; i < str1.Length; i++)
{
for (int j = 0; j < str2.Length; j++)
{
if (str1[i] != str2[j])
num[i, j] = 0;
else
{
if ((i == 0) || (j == 0))
num[i, j] = 1;
else
num[i, j] = 1 + num[i - 1, j - 1];
if (num[i, j] > maxlen)
{
maxlen = num[i, j];
int thisSubsBegin = i - num[i, j] + 1;
if (lastSubsBegin == thisSubsBegin)
{//if the current LCS is the same as the last time this block ran
sequenceBuilder.Append(str1[i]);
}
else //this block resets the string builder if a different LCS is found
{
lastSubsBegin = thisSubsBegin;
sequenceBuilder.Length = 0; //clear it
sequenceBuilder.Append(str1.Substring(lastSubsBegin, (i + 1) - lastSubsBegin));
}
}
}
}
}
sequence = sequenceBuilder.ToString();
return maxlen;
}
public int LongestCommonSubstring(字符串str1、字符串str2、out字符串序列)
{
sequence=string.Empty;
if(String.IsNullOrEmpty(str1)| | String.IsNullOrEmpty(str2))
返回0;
int[,]num=新int[str1.Length,str2.Length];
int maxlen=0;
int=0;
StringBuilder sequenceBuilder=新的StringBuilder();
for(int i=0;imaxlen)
{
maxlen=num[i,j];
int=i-num[i,j]+1;
如果(lastSubsBegin==thisSubsBegin)
{//如果当前LCS与上次运行此块时相同
sequenceBuilder.Append(str1[i]);
}
else//如果找到不同的LCS,此块将重置字符串生成器
{
lastSubsBegin=此SUBSBEGIN;
sequenceBuilder.Length=0;//清除它
sequenceBuilder.Append(str1.Substring(lastsubsbeing,(i+1)-lastsubsbeing));
}
}
}
}
}
sequence=sequenceBuilder.ToString();
返回maxlen;
}
提示:这是一个三重嵌套循环,将候选字符串中的每个字符与每个目标字符串中的每个字符进行比较。它还具有与#1相同的F
,以及与所有三个字符串相同的b
。确切的标准是什么?事实上它有“FIZZb”的共同点,不是吗?@Matthew:有多少个字符构成匹配?对于fizblahfizblah
(FIZ
是一个交集,FIZZb
是另一个交集)提示:这是一个三重嵌套循环,将候选字符串中的每个字符与每个目标字符串中的每个字符进行比较。它还具有与#1相同的F
,以及与所有三个字符串相同的b
。确切的标准是什么?事实上它有“FIZZb”的共同点,不是吗?@Matthew:有多少个字符构成匹配?对于fizblahfizblah
(FIZ
是一个交叉点,FIZZb
是另一个交叉点)你希望得到什么样的结果那个有趣的小U
是什么意思?看起来甚至不像有效的Pascal。@RobertHarvey它只是复制并粘贴的伪代码这看起来很有希望@cost,我同意,我不确定这是否构成了一个c#问题的答案。这个有趣的小U
是什么意思?看起来甚至不像是有效的Pascal。@RobertHarvey它只是复制和粘贴的伪代码这看起来很有希望@成本,我同意,不确定这个公司