Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 在比较两个唯一字符串时,如何检测模式匹配?_String_Algorithm_Pattern Matching_Automaton_Rabin Karp - Fatal编程技术网

String 在比较两个唯一字符串时,如何检测模式匹配?

String 在比较两个唯一字符串时,如何检测模式匹配?,string,algorithm,pattern-matching,automaton,rabin-karp,String,Algorithm,Pattern Matching,Automaton,Rabin Karp,我正在寻找以下字符串模式匹配问题的解决方案 我们有一个函数,它接受两个参数:pattern和input,它们都是字符串 让我们假设模式:aabbaa和输入:catdogdogcatcat 这些特定参数将被视为匹配,因为input的字符中有一个模式,并且该模式与pattern中的单词模式匹配 返回一个布尔值,以指示哪里有匹配项。 上面给出的示例将返回1 function (pattern, input) { // patterns within the input string are elu

我正在寻找以下字符串模式匹配问题的解决方案

我们有一个函数,它接受两个参数:pattern和input,它们都是字符串

让我们假设
模式:aabbaa
输入:catdogdogcatcat

这些特定参数将被视为匹配,因为
input
的字符中有一个模式,并且该模式与
pattern
中的单词模式匹配

返回一个
布尔值
,以指示哪里有匹配项。 上面给出的示例将返回
1

function (pattern, input) {
  // patterns within the input string are elucidated from input
  // those patterns are checked against the pattern
  return boolean
}

创建要检查的嵌套循环

int i =0;
char tester [] = "xyzfffasdfsadf";
bool checker = false;
while (tester[i] != '\0')
{
    if (tester [i] == 'x')
    {
        i++;
       if (tester[i] == 'y')
       {
          i++;
          if (tester[i] == 'z')
          {
             checker = true;
          }
       }
   }else {
    i++;
   }
}
if(checker == true){
cout << "Pattern exist";
}
inti=0;
字符测试仪[]=“XYZFFASDFSADF”;
bool-checker=false;
而(测试仪[i]!='\0')
{
如果(测试仪[i]=“x”)
{
i++;
如果(测试仪[i]=“y”)
{
i++;
如果(测试仪[i]=“z”)
{
checker=true;
}
}
}否则{
i++;
}
}
if(checker==true){
cout一般化的问题“为给定的字符串找到模式”要困难得多,因为一个字符串可以符合多个模式。例如

catcatdogcat
符合多种模式。下面是一个非详尽的列表:

aaba           cat cat dog cat
a              catcatdogcat
ab             catcat dogcat
abcabcefgabc   c a t c a t d o g c a t
ababcab        ca t ca t dog ca t
因此,我认为“找到所有模式,然后看看提议的模式是否在其中”的方法不会奏效

这意味着我们可能希望使用建议的模式作为指导,尝试分解字符串,但我也不完全确定这会是什么样子

在特定情况下,当模式以相同的子字符串开始和结束时(例如在
aaba
中),我认为您可以从字符串的开始和结束开始,每次使用一个字符,直到获得匹配:

catcatdogcat
CatcatdogcaT
CAtcatdogcAT
CATcatdogCAT <-- Substring "CAT" is a candidate for "a". Check if that pattern matches.
我想那会管用的。很明显,这个伪代码有很多细节,效率不高,但它能完成任务。

我是这样做的

def check_pattern(s1,s2):
    i=j=97
    p1=p2=""

    for index1,l1 in enumerate(s1):
        if l1 not in s1[0:index1]:
            p1+=chr(i)
            i+=1
        else:
            p1+= chr(97+s1.index(l1))

    
    for index2,l2 in enumerate(s2):
        if l2 not in s2[0:index2]:
            p2+=chr(j)
            j+=1
        else:
            p2+= chr(97+s2.index(l2))

    if p1==p2:
        return True
    else:
        return False

z=check_pattern("aab","xxz")
print(z)

这里的标准是什么?模式可以是
aabc
,以你为例。谢谢你的否决票'n破折号:)@st0le我会重新处理这个问题-
aabc
不是正确的模式仍然不是一个好问题,那么为什么它有-2?欣赏这个想法。但是解决方案需要能够处理任何字符串,而不仅仅是字符串包含x、y和z。此外,该解决方案可能更有效。该解决方案中,可以替换x、y、z。x、y、z是要检测的模式的表示形式。因此,实际上可以使用多个单独的计数器来检测多个模式,并在识别模式时增加计数器。是的,我会ld同意该解决方案可能更有效。感谢您的深思熟虑。今晚我将尝试一下。虽然此代码可能会解决此问题,但它如何以及为什么解决此问题将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,您是在回答读者的问题将来,不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。
checkpattern(pattern, string) :=
  if (pattern has just one letter) 
    return true
  if (pattern has more than one letter, but it's one character repeated)
    return whether the string repeats that way
  for (each substring from the start of the string of length x=[0..])
    does the string match this candidate substring following the pattern?
    if (yes)
      if (checkpattern(pattern - letter, string - substring))
        return true
      else
        continue
    if (no)
      continue
  return false
def check_pattern(s1,s2):
    i=j=97
    p1=p2=""

    for index1,l1 in enumerate(s1):
        if l1 not in s1[0:index1]:
            p1+=chr(i)
            i+=1
        else:
            p1+= chr(97+s1.index(l1))

    
    for index2,l2 in enumerate(s2):
        if l2 not in s2[0:index2]:
            p2+=chr(j)
            j+=1
        else:
            p2+= chr(97+s2.index(l2))

    if p1==p2:
        return True
    else:
        return False

z=check_pattern("aab","xxz")
print(z)