poj 1056 IMMEDIATE DECODABILITY

题目链接: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\n", 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", n );
            else
                printf( "Set %d is immediately decodable\n", n );

            Count = 0;
            flag = -1;
            continue;
        }

        Count++;
    }

    return 0;
}
时间: 2024-10-12 02:59:45

poj 1056 IMMEDIATE DECODABILITY的相关文章

POJ 1056 IMMEDIATE DECODABILITY (字典树)

IMMEDIATE DECODABILITY Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12972   Accepted: 6222 Description An encoding of a set of symbols is said to be immediately decodable if no code for one symbol is the prefix of a code for another s

POJ 1056 IMMEDIATE DECODABILITY Trie 字符串前缀查找

POJ1056 给定若干个字符串的集合 判断每个集合中是否有某个字符串是其他某个字符串的前缀 (哈夫曼编码有这个要求) 简单的过一遍Trie就可以了 #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<vector> #include<queue> #include<algor

【POJ】1056 IMMEDIATE DECODABILITY

字典树水题. 1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 5 typedef struct Trie { 6 bool v; 7 Trie *next[2]; 8 } Trie; 9 10 Trie *root; 11 12 bool create(char str[]) { 13 int i = 0, id; 14 bool ret = false; 15 Trie *p = root

poj 1056 Trie树判断哈夫曼编码是否合法

理解了Trie树然后就能1A   其实估计这个题随便做做就能A掉,可能不需要高级数据. 先贴吉林大学的代码模板 /*==================================================*| Trie树(k叉) | INIT: init(); | 注: tree[i][tk]>0时表示单词存在, 当然也可赋予它更多含义; \*==================================================*/ const int tk = 26,

POJ 1056

#include <iostream> #include <string> #define MAXN 50 using namespace std; struct node { node * l; node * r; bool boo; node() { l = NULL; r = NULL; boo = false; } }; bool ans; int _index; node res[2*MAXN+1]; node * insert(node * root,string s,

poj1056 &amp; hdu1305 &amp; zoj1808 Immediate Decodability(字典树变形)

题目链接 poj  1056 :http://poj.org/problem?id=1056 hdu 1305 :http://acm.hdu.edu.cn/showproblem.php?pid=1305 zoj  1808 :http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=808 Description An encoding of a set of symbols is said to be immediately de

【暖*墟】 #trie# 字典树的运用

Trie,又称字典树 是一种用于实现字符串快速检索的多叉树结构. 每个节点都拥有若干个字符指针,若在插入或检索字符串时扫描到一个字符c , 就沿着当前节点的c这个字符指针,走向该指针指的节点. 下面我们来详细讨论Trie的基本操作过程. 初始化 一棵空Trie仅包含一个根节点,该点的字符指针均指向空. *********  根节点表示空串.********* 插入 当需要插入一个字符串S时,我们令一个指针P起初指向根节点. 然后,依次扫描S中的每个字符c. 1.若P的c字符指针指向一个已经存在的

acm学习

要学习的   基本结构 高级结构 题单 集合结构   幷查集 POJ 1182 POJ 1308 POJ 1611 POJ 1986 POJ 1988 线性结构 数组 栈 队列 双端队列 POJ POJ POJ POJ POJ 树状结构 二叉树 BST AVL树 splay树(伸展树) Treap Cartesian Tree Size Balance Tree POJ 3580(splay tree) POJ 2761(Treap) POJ 2201(Cartesian Tree) POJ 3

字典树trie的学习与练习题

博客详解: http://www.cnblogs.com/huangxincheng/archive/2012/11/25/2788268.html http://eriol.iteye.com/blog/1166118 http://www.360doc.com/content/12/1116/15/9615799_248213540.shtml http://www.cnblogs.com/tanky_woo/archive/2010/09/24/1833717.html http://bl