UVa 11732 - strcmp() Anyone

UVa 11732 - strcmp() Anyone?

题目:给你一些单词,给你一个字符串比较函数,所有单词进行比价时,比较的次数是多少。

分析:字符串、字典树。首先,看数据规模,发现如果使用正常的字典树,会TLE和MLE。

            因为,常规的字典树深度为1000,而且有可能会有大量的指正空间浪费。

            所以,采用树的压缩算法(左兄弟,右孩子)。可以提高运行效率。

            然后,在字典树上同级即可。统计时,可以先建树再统计,或者边建树边统计。

            这里我选用边建树边统计的方法(网上大量的做法,都是先建树再统计的,搜索求解)

            每次插入单词时,它只与前面插入的单词比较;单词的比较次数为其中每个字母的比较次数的和。

            单词中每个字母的比较次数,就是这个字母的根节点包含的单词个数。

            单词比较函数如下:

int strcmp(char *s, char *t)
{
    int i;
    for (i=0; s[i]==t[i]; i++)
        if (s[i]=='\0')
            return 0;
    return s[i] - t[i];
}
            因为比较函数如上,计算时要注意以下几点:

            1.相同长度为L的两个单词比较代价为2L-1,出最后一次外要进行s结束的判断;

            2.单词的比较长度应该为strlen(str)+1,结束符也是比较的一环;

            3.如果两个单词相同,则多计算一次结束判断,比较次数+1。

注意:使用LL,否则数据会溢出。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>

using namespace std;

typedef long long LL;

char words[1010];

/* Trie define */  
#define nodesize 4444444    //节点个数 
  
typedef struct node1  
{
	char    value;
	int     size;
	int     count;
    node1*  Lchild;
	node1*  Rchild;
}tnode;  
tnode dict[nodesize];
  
class Trie  
{  
    private:  
		LL     count;
        int    size;  
        tnode* root;
    public:  
        Trie() {initial();}   
        void initial() {  
            memset( dict, 0, sizeof(dict) );  
            size=0;count=0LL;root=newnode(0);
        }  
        tnode* newnode( char c ) {
			dict[size].value = c;
			return &dict[size ++];
		}  
        void insert( char* word, int L ) {  
            tnode* now = root->Rchild,*save = root; 
            int same = 1;
            for ( int i = 0 ; i <= L ; ++ i ) { 
				if ( !now ) {
					save->Rchild = newnode(word[i]);
					now = save->Rchild;
					now->count = 1;
					same = 0;
				}else {
					if ( i ) count += now->count;
					count += now->count ++;
					while ( now->Lchild && now->value != word[i] )
						now = now->Lchild;
					if ( now->value != word[i] ) {
						now->Lchild = newnode(word[i]); 
						now = now->Lchild;
						same = 0;
					}
				}
				save = now;
				now = save->Rchild;
            }
            if ( same ) save->size ++;
            count += save->size;
        }  
        LL query() {return count;}
}trie;  
/* Trie  end */  

int main()
{
	int Case = 1,N;
	while ( scanf("%d",&N) != EOF ) {
		if ( !N ) break;
		
		trie.initial();
		for ( int i = 0 ; i < N ; ++ i ) {
			scanf("%s",words);
			trie.insert(words,strlen(words));
		}
		
		printf("Case %d: %lld\n",Case ++,trie.query());
	}
	return 0;
}