2

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

基本上我正在寻找具有特定模式的单词(没关系)。我有一个特殊的函数来确定它,如果单词满足要求,它返回 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个小时...

4

4 回答 4

1

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

如果您不熟悉 RegEx,以下站点是一个很好的起点/参考。http://www.regular-expressions.info/quickstart.html

您将使用由三个部分组成的表达式。以下语法来自内存,因此您需要仔细检查:

  • (first)- 匹配单词'first'
  • [\w]*{1,3}- 任何重复的单词,例如 1 到 3 次 -
  • (second)- 匹配单词“秒”
于 2012-08-25T18:32:00.260 回答
1

请原谅我没有使用伪代码,而是使用了实际代码。我希望我现在能很好地理解这个问题,以至于我认为它看起来并不复杂,是准确的。

# 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"  ) ;
    }
}

使用此输入:

barf   barf  barf   healthy     feeling     better   barf  barf barf uh oh sick again
barf   barf  healthy     feeling     better   barf  barf uh oh sick again
barf   healthy     barf   feeling     better     barf   uh   barf   oh sick again
barf   healthy     feeling     better   barf uh oh sick again

我得到了这个输出:

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

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

于 2012-08-25T19:54:42.350 回答
0

像这样的东西?

for (i = 0; i<wordcount; i++)
{
    CurrentWord = Words[i]
    if (WordMatchesCritera(CurrentWord))
    {
        if (HavePrecedingWord)
        { 
             success !!!
        } 
        else
        {
            i ++;
            HavePrecedingWord = true
        }
    }
    else
    {
        HavePrecedingWord = false;
    }
}
于 2012-08-25T18:12:02.790 回答
0

这行得通吗?

    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  ? */
            }

          }
    }
于 2012-08-25T19:35:56.647 回答