首页 > 其他 > 详细

poj 3461 Oulipo

时间:2014-10-20 21:04:10      阅读:183      评论:0      收藏:0      [点我收藏+]

题目链接:http://poj.org/problem?id=3461

 

思路:

      字符串匹配问题,使用KMP算法解决。

代码:

#include <stdio.h>

char T[1000005], W[10005];
int Next[10005];
int Len_T, Len_W;

void GetNext( )
{
    int i = 0, j = -1;

    Next[0] = -1;
    if ( W[0] == \0 )
        return;

    while ( i < Len_W )
    {
        if ( j == -1 || W[i] == W[j] )
        {
            ++i;
            ++j;
            Next[i] = j;
        }
        else
            j = Next[j];
    }
}

int Matcher( )
{
    int i = 0, j = 0, Count = 0;

    while ( i < Len_T )
    {
        if ( j == -1 || T[i] == W[j] )
        {
            ++i;
            ++j;
        }
        else
            j = Next[j];

        if( j == Len_W )
            Count++;
    }

    return Count;
}


int main( )
{
    int n;

    scanf( "%d\n", &n );
    while ( n-- )
    {
        int ans;
        char Tmp;
        Len_T = Len_W = 0;

        while ( ( Tmp = getchar( ) ) != \n )
            W[Len_W++] = Tmp;
        W[Len_W] = \0;

        while ( ( Tmp = getchar( ) ) != \n )
            T[Len_T++] = Tmp;
        T[Len_T] = \0;

        GetNext( );        
        ans = Matcher( );
        printf( "%d\n", ans );
    }

    return 0;
}

 

poj 3461 Oulipo

原文:http://www.cnblogs.com/tallisHe/p/4038354.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!