c - 允许跟踪具有特定模式的单词后两个单词出现的单词的算法

标签 c algorithm pseudocode

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

基本上我正在寻找具有特定模式的单词(不管是什么)。我有一个特殊的函数来确定它,如果该单词满足要求,它会返回 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 个小时来解决这个问题...

最佳答案

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

# 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

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

关于c - 允许跟踪具有特定模式的单词后两个单词出现的单词的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12124406/

相关文章:

algorithm - 从 B-Tree 中删除 - M=5

r - 如何计算矩阵的总和?

algorithm - 根据给定的标准找到图中两个节点之间的路径 - 优化任务

c - GetFileSizeEx 在 System32 目录上返回不正确的值

c - 返回值在 C 中未定义

algorithm - 最优柔性盒布局算法

algorithm - 计算星期几的日期

c - 无法使用 GCC/Mingw32 编译器在 C 中使用 "Alt+Space"发送虚拟 key "SendInput()"?

c - 我正在尝试用C编程语言创建一个登录系统

支持快速近似最近邻查询的数据库