zoukankan      html  css  js  c++  java
  • poj 1056 IMMEDIATE DECODABILITY(KMP)

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

    思路分析:检测某字符串是否为另一字符串的前缀,数据很弱,可以使用暴力解法。这里为了练习KMP算法使用了KMP算法。

     

    代码如下:

    #include <iostream>
    using namespace std;
    
    const int N = 10;
    const int Len = 20;
    char A[N][Len];
    int Next[N][Len];
    
    void get_nextval( char P[], int Next[] )
    {
        int i = 0, j = -1;
        int PLen = strlen(P);
    
        Next[0] = -1;
        while ( i < PLen - 1 )
        {
            if ( j == -1 || P[i] == P[j] )
            {
                i++;
                j++;
                if ( P[i] == P[j] )
                    Next[i] = j;
                else
                    Next[i] = Next[j];
            }
            else
                j = Next[j];
        }
    }
    
    int KMP_Matcher( char T[], char P[], int Next[] )
    {
        int i = 0, j = 0;
        int TLen = strlen( T );
        int PLen = strlen( P );
    
        while ( i < TLen && j < PLen )
        {
            if ( j == -1 || T[i] == P[j] )
            {
                i++;
                j++;
            }
            else
                j = Next[j];
        }
    
        if ( j == PLen )
            return i - j;
        else
            return -1;
    }
    
    
    int main( )
    {
        int Count = 0, flag = -1, n = 0;
    
        while ( scanf( "%s
    ", A[Count] ) != EOF )
        {
            if ( A[Count][0] == '9' )
            {
                n++;
                for( int i = 0; i < Count; ++i )
                    get_nextval( A[i], Next[i] );
    
                for ( int i = 0; i < Count - 1; ++i )
                    for ( int j = i + 1; j < Count; ++j )
                    {
                        if ( flag == 0 )
                            break;
                        flag = KMP_Matcher( A[j], A[i], Next[i] );
                    }
    
                if ( flag == 0 )
                    printf( "Set %d is not immediately decodable
    ", n );
                else
                    printf( "Set %d is immediately decodable
    ", n );
                
                Count = 0;
                flag = -1;
                continue;
            }
    
            Count++;
        }
        
        return 0;
    }
  • 相关阅读:
    关于接口是值类型还是引用类型的猜测
    絮语工作四年的碎碎念
    烧钱游戏加入创业公司的一些感想
    关于C#调用非托管动态库方式的性能疑问
    couchbase作为分布式session容器时的注意事项
    poj3624
    明天的下载链接
    poj 1502
    poj1459 多源多汇最大流
    poj 3041
  • 原文地址:https://www.cnblogs.com/tallisHe/p/4049474.html
Copyright © 2011-2022 走看看