C 一种算法,允许跟踪出现在具有特定模式的单词后两个单词的单词

C 一种算法,允许跟踪出现在具有特定模式的单词后两个单词的单词,c,algorithm,pseudocode,C,Algorithm,Pseudocode,我非常感谢您对该算法/伪代码的帮助 基本上,我在寻找具有特定模式的单词(不管是什么)。我有一个特殊的函数来确定它,如果单词满足要求,它将返回1。 当它这样做时,后面的第二个字应该被省略,而不是保存在输出中。 当“选择的”单词被一个“未选择的”单词隔开时,我对此没有问题。 问题是——当“被选中的”一个接一个出现时该怎么办 我准备了这样一个伪代码来澄清一下情况。但不幸的是,它并不适用于“选择”和“未选择”的所有组合 我介绍了三个计数器/变量,帮助我发现当前的位置 下面的伪代码不符合逻辑顺序 if (

我非常感谢您对该算法/伪代码的帮助

基本上,我在寻找具有特定模式的单词(不管是什么)。我有一个特殊的函数来确定它,如果单词满足要求,它将返回1。 当它这样做时,后面的第二个字应该被省略,而不是保存在输出中。 当“选择的”单词被一个“未选择的”单词隔开时,我对此没有问题。 问题是——当“被选中的”一个接一个出现时该怎么办

我准备了这样一个伪代码来澄清一下情况。但不幸的是,它并不适用于“选择”和“未选择”的所有组合

我介绍了三个计数器/变量,帮助我发现当前的位置

下面的伪代码不符合逻辑顺序

if (counter == 2 || in_a_row >= 3) {
    erase = 1;
    counter--;
    yes = 0;
    if (!chosen) 
        counter = 0;
}
if (chosen) {
    counter++;
    yes = 1;
    in_a_row++;
} else {
    if (yes = 1) /* yes - is used when the preceeding word is chosen and the next is not chosen, in order to keep track of the counter */
        counter++;
}
if (in_a_row == 5)
    in_a_row = 4; /* just to make sure that counter doesn't go too high */
if (erase == 1)
    /*erasing procedure*/
如果你有一个更简单的想法,或者看到其中的错误,请帮助我。 试着用8个小时解决这个问题…

像这样的事情

for (i = 0; i<wordcount; i++)
{
    CurrentWord = Words[i]
    if (WordMatchesCritera(CurrentWord))
    {
        if (HavePrecedingWord)
        { 
             success !!!
        } 
        else
        {
            i ++;
            HavePrecedingWord = true
        }
    }
    else
    {
        HavePrecedingWord = false;
    }
}

(i=0;i的
听起来像是正则表达式的经典用法。您没有指定语言,但许多语言都支持正则表达式

如果您不熟悉RegEx,以下站点是它的良好起点/参考

您将使用由三部分组成的表达式。下面的语法来自内存,因此您需要再次检查它:

  • (first)
    -匹配单词“first”
  • [\w]*{1,3}
    -任何重复1到3次的单词-
  • (second)
    -匹配单词“second”
    • 这行吗

          matchID = -1;
          eraseID = -1;
      
      
          for(i = 0; i < ... ; i++)
          {
               if( wordMatches ( word[i] ) )
               {                  
                  matchID = i;     /* found the chosen one */
                  eraseID = -1;         
               }
               else 
               {
                  if( matchID != -1 ) /* chosen one was found ? */
                  {
                       eraseID = i;   /* erase the next non-matching one */
                       break; /* ? done for now  ? */
                  }
      
                }
          }
      
      matchID=-1;
      橡皮擦ID=-1;
      对于(i=0;i<…;i++)
      {
      if(单词匹配(单词[i]))
      {                  
      matchID=i;/*找到了所选的一个*/
      橡皮擦ID=-1;
      }
      其他的
      {
      如果找到(matchID!=-1)/*所选的一个*/
      {
      擦除ID=i;/*擦除下一个不匹配的*/
      中断;/*?现在完成了吗*/
      }
      }
      }
      
      请原谅,我没有使用伪代码,而是使用了实际代码。我希望我现在对这个问题有足够的了解,我相信它看起来并不复杂,是准确的

      # include <stdio.h>
      # include <ctype.h>
      # include <string.h>
      
      
      # define BUFF_SIZE       1024
      # define WORD_DELIM     " "
      # define MATCH_PATT     "barf"
      
      
      int main(  int ac ,  char *av[]  )
      {
          __u_char    false = ( 1 == 0 ) ;
          __u_char    true = ( 1 == 1 ) ;
      
          __u_char    match_1_back = false ;
          __u_char    match_2_back = false ;
      
          char        line_buff[  BUFF_SIZE  ] ;
          char        *buff_ptr ;
          char        *word_ptr ;
      
      
          while (  fgets( line_buff ,  BUFF_SIZE ,  stdin )  )
          {
              puts(  "\nInput line was:  "  ) ;
              puts(  line_buff  )  ;
      
              puts(  "Output line is:  "  ) ;
      
              buff_ptr = line_buff ;
      
              while (  ( word_ptr = strtok( buff_ptr ,  WORD_DELIM )  )  !=  NULL  )
              {
                  buff_ptr = NULL ;
      
                  if (  strcmp( word_ptr ,  MATCH_PATT  )  ==  0  )
                  {
                      // Set these to what they should be for next iteration.
                      match_2_back = match_1_back ;
                      match_1_back = true ;
      
                      // Don't output matched token.
                  }
                  else
                  {
                      // Don't output token if a token matched 2 tokens back.
                      if (  ! match_2_back  )
                          printf(  "%s " ,  word_ptr  ) ;
      
                      // Set these to what they should be for next iteration.
                      match_2_back = match_1_back ;
                      match_1_back = false ;
                  }
              }
      
              printf(  "\n"  ) ;
          }
      }
      
      我得到了这个输出:

      Input line was:  
      barf   barf  barf   healthy     feeling     better   barf  barf barf uh oh sick again
      
      Output line is:  
      better sick again
      
      
      Input line was:  
      barf   barf  healthy     feeling     better   barf  barf uh oh sick again
      
      Output line is:  
      better sick again
      
      
      Input line was:  
      barf   healthy     barf   feeling     better     barf   uh   barf   oh sick again
      
      Output line is:  
      healthy feeling uh oh again
      
      
      Input line was:  
      barf   healthy     feeling     better   barf uh oh sick again
      
      Output line is:  
      healthy better uh sick again
      

      我只是使用了一个简单的比较,而不是实际的正则表达式。我只是想说明一下算法。输出是否符合要求?

      它完全不起作用。想象一下顺序:select not not not not not not not not not not not not not not not not not not not not not not not not not not not not not not not not not not not not not not not notksanyway@peterkowalski不-i++会跳过Chosen2,我不知道是谁+1,但这只是一个软件广告。不,谢谢,我需要使用纯C。嗨,Peter,我只是想澄清一下,这不是一个软件广告。正则表达式是许多编程语言的内置功能。我提供的链接不是我的附属网站,而是一个t我经常使用它作为参考。不管怎样,我很高兴你找到了一个解决方案。我没有完全了解一个单词应该删除或不应该删除的所有条件。因此,这只是一个一般性的评论。假设你在开始算法之前拥有完整的单词列表(例如,你没有实时使用键盘输入),我遇到类似问题的经验是,你必须从列表的后面删除到顶部。想法是:(1)将单词放在列表上,然后(2)遍历整个列表,(3)标记要删除的元素,(4)向后运行列表擦除元素。向后擦除使簿记更容易。@rpsml我可以毫无问题地继续处理文件,唯一的问题是如何决定省略哪个词。实际上,我不理解这个问题,因为我认为它很小(我可能遗漏了什么).我对零级算法的理解是:如果触发字位于位置
      i
      ,只需标记位置
      i+2
      ,就可以擦除。就是这样。这将导致出现这样的情况,即
      i
      i+1
      处的触发字将擦除
      i+2
      i+3
      处的字。
      i
      处的触发字>,
      i+1
      i+2
      将删除
      i+2
      (这本身就是一个触发字)、
      i+3
      i+4
      处的字。我缺少什么?谢谢!使用“已选择”这个词听起来好像是选择了这个词作为输出。尽管您的实现与任务中的实现略有不同。我想说,我很高兴您帮助了我,基于此,我创建了自己的程序,现在工作起来很有魅力。祝您好运!
      Input line was:  
      barf   barf  barf   healthy     feeling     better   barf  barf barf uh oh sick again
      
      Output line is:  
      better sick again
      
      
      Input line was:  
      barf   barf  healthy     feeling     better   barf  barf uh oh sick again
      
      Output line is:  
      better sick again
      
      
      Input line was:  
      barf   healthy     barf   feeling     better     barf   uh   barf   oh sick again
      
      Output line is:  
      healthy feeling uh oh again
      
      
      Input line was:  
      barf   healthy     feeling     better   barf uh oh sick again
      
      Output line is:  
      healthy better uh sick again