zoukankan      html  css  js  c++  java
  • HDU 4300 Clairewd’s message (next函数的应用)

    题意:给你一个明文对密文的字母表,在给你一段截获信息,截获信息前半段是密文,后半段是明文,但不清楚它们的分界点在哪里,密文一定是完整的,明文可能是残缺的,求完整的信息串(即完整的密文+明文串)。

    题解:KMP next函数的应用。

    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    
    const int MAXN = 100010;
    
    char table[32];
    char extable[32];
    char ori[MAXN];
    char aft[MAXN];
    int next[MAXN];
    int  len;
    
    void init()
    {
        for ( int i = 0; i < 26; ++i )
            extable[ table[i]-'a' ] = 'a' + i;
    
        len = strlen(ori);
        for ( int i = 0; i < len/2; ++i )
            aft[i] = ori[i];
    
        for ( int i = len/2; i < len; ++i )
            aft[i] = table[ ori[i] - 'a' ];
    
        aft[len] = '';
    
        return;
    }
    
    void getNext( char* s, int* next )
    {
        int length = len;
        int i = 0, j = -1;
        next[0] = -1;
        while ( i < length )
        {
            if ( j == -1 || s[i] == s[j] )
            {
                ++i, ++j;
                next[i] = j;
            }
            else j = next[j];
        }
    }
    
    int main()
    {
        int T;
        scanf( "%d", &T );
        while ( T-- )
        {
            scanf( "%s", table );
            scanf( "%s", ori );
            init();
            getNext( aft, next );
    
            int ans = len;
            //printf("next[%d] = %d
    ", ans, next[ans] );
            while ( next[ans] > len/2 ) ans = next[ans];
            ans = len - next[ans];
            //printf( "ans = %d
    ", ans );
            for ( int i = 0; i < ans; ++i )
                printf( "%c", ori[i] );
            for ( int i = 0; i < ans; ++i )
                printf( "%c", extable[ ori[i] - 'a' ] );
            puts("");
        }
        return 0;
    }
  • 相关阅读:
    锐浪报表应用系列二
    论产品和项目
    我的处女作
    今天晚上吃什么?
    今日晚餐
    PYTHON+数据库
    周末看到小区有个阿姨溜羊驼
    AD 10使用技巧---新学习
    使用.NET进行高效率互联网敏捷开发的思考和探索【一、概述】
    【开发随感】【一】【开发基础的基础】
  • 原文地址:https://www.cnblogs.com/GBRgbr/p/3581164.html
Copyright © 2011-2022 走看看