Secret Message ---- (Trie树应用)

Secret Message

 
总时间限制: 
2000ms 
内存限制: 
32768kB
描述
Bessie is leading the cows in an attempt to escape! To do this, the
cows are sending secret binary messages to each other.

Ever the clever counterspy, Farmer John has intercepted the first
b_i (1 <= b_i <= 10,000) bits of each of M (1 <= M <= 50,000) of
these secret binary messages.

He has compiled a list of N (1 <= N <= 50,000) partial codewords
that he thinks the cows are using. Sadly, he only knows the first
c_j (1 <= c_j <= 10,000) bits of codeword j.

For each codeword j, he wants to know how many of the intercepted
messages match that codeword (i.e., for codeword j, how many times
does a message and the codeword have the same initial bits).  Your
job is to compute this number.

The total number of bits in the input (i.e., the sum of the b_i and
the c_j) will not exceed 500,000
输入
INPUT FORMAT:

* Line 1: Two integers: M and N

* Lines 2..M+1: Line i+1 describes intercepted code i with an integer
b_i followed by b_i space-separated 0's and 1's

* Lines M+2..M+N+1: Line M+j+1 describes codeword j with an integer
c_j followed by c_j space-separated 0's and 1's
输出
* Lines 1..M: Line j: The number of messages that the jth codeword
could match.
样例输入
4 5
3 0 1 0
1 1
3 1 0 0
3 1 1 0
1 0
1 1
2 0 1
5 0 1 0 0 1
2 1 1
样例输出
1
3
1
1
2
提示
INPUT DETAILS:

Four messages; five codewords.
The intercepted messages start with 010, 1, 100, and 110.
The possible codewords start with 0, 1, 01, 01001, and 11.

0 matches only 010: 1 match
1 matches 1, 100, and 110: 3 matches
01 matches only 010: 1 match
01001 matches 010: 1 match
11 matches 1 and 110: 2 matches
思路: Trie树的应用,注意输入的如果不是有长度一样的,需要特殊处理一下,
我是先记录一下第一个点出现的个数,然后找孩子节点,找的过程中减去孩子节点的兄弟节点的个数,
就得出了答案。

 

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<malloc.h>
using namespace std;
typedef struct node{
	int v;
	node *next[2];
}Trie;
Trie root;
void createTrie(char *str)
{
    int len = strlen(str);
    Trie *p = &root, *q;
    for(int i=0; i<len; ++i)
    {
        int id = str[i]-'0';
        if(p->next[id] == NULL)
        {
            q = (Trie *)malloc(sizeof(root));
            q->v = 1;
            for(int j=0; j<2; ++j)
                q->next[j] = NULL;
            p->next[id] = q;
            p = p->next[id];
        }
        else
        {
            p->next[id]->v++;
            p = p->next[id];
        }
    }
}

int findTrie(char *str)
{
    int len = strlen(str);
       Trie *p = &root;
       Trie *other;
    if(len==1){
    	return 	p->next[str[0]-'0']->v;
    }
    int total =p->next[str[0]-'0']->v;
    p = p->next[str[0]-'0'];
    for(int i=1;i<len;i++){
		if(p->next[(str[i]-'0')^1]!=NULL){ // 下一个节点的兄弟节点不空,要减去 
			total=total-p->next[(str[i]-'0')^1]->v;
		}
		if(i==len-1){
			break;
		}
		if(p->next[str[i]-'0']!=NULL){ //下一个节点不空,遍历 
			p=p->next[str[i]-'0'];
		}
		else if(p->next[str[i]-'0']==NULL){
		    break;
		} 
    }
    return total;
}

int main(){
	int n,m,i,j,k,ans,num;
	char str[50001];
	scanf("%d%d",&n,&m);
	for(i=0;i<n;i++){
		scanf("%d",&num);
		memset(str,'0',sizeof(str));
		for(j=0;j<num;j++){
			scanf("%d",&k);
			str[j]=k+'0';
		}
		str[j]='\0'; 
		createTrie(str);
	}
	for(i=0;i<m;i++){
		scanf("%d",&num);
	    memset(str,'0',sizeof(str));
	    for(j=0;j<num;j++){
			scanf("%d",&k);
			str[j]=k+'0';
	    }
	    str[j]='\0';
	    ans = findTrie(str);
	    printf("%d\n",ans);
	}
	return 0;
} 

  

转载于:https://www.cnblogs.com/Koaler/p/8766080.html

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 、 1资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READmE.文件(md如有),本项目仅用作交流学习参考,请切勿用于商业用途。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值