Tire习题——洛谷(P2580 ^ P2922)

P2580 于是他错误的点名开始了

在这里插入图片描述
在这里插入图片描述

解答一

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=1000001;
int son[N][26],tot=1,cnt[N],x;
void insert(char* str)
{
	int len=strlen(str),p=1;
	for(int k=0;k<len;k++)
	{
		int u=str[k]-'a';
		if(son[p][u]==0) son[p][u]= ++tot;
		p=son[p][u];
	}
	cnt[p]=1;
}
int search(char* str)
{
	int len=strlen(str),p=1;
	for(int k=0;k<len;k++)
	{
		p=son[p][str[k]-'a'];
		if(p==0)
		return 0;
	}
	x=p;
	return cnt[p];
}
int n;
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		char str[N];
		cin>>str;
		insert(str);
	}
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		char str[N];
		cin>>str;
		if(search(str)==1)
		{
			printf("OK\n");
			cnt[x]++;
		}
		else
		if(search(str)>1)
		printf("REPEAT\n");
		else
		printf("WRONG\n");
	}
	return 0;
}

解答二

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 500010;
int son[N][26], InsertCnt[N], QueryCnt[N], idx;
char str[N];
void insert(char str[])
{
    int p = 0;
    for(int i = 0; str[i]; i++)
    {
        int u = str[i] - 'a';
        if(!son[p][u]) son[p][u] = ++idx;
        p = son[p][u];
    }
    InsertCnt[p]++;
}
int query(char str[])
{
    int p = 0;
    for(int i = 0; str[i]; i++)
    {
        int u = str[i] - 'a';
        if(!son[p][u]) return 0;//如果某个字符不存在,返回0
        p = son[p][u];
    }
    if(!InsertCnt[p]) return 0;//循环完后,没有结束标记,则没有这个串,返回0
    else if(!QueryCnt[p])//有结束标记,看查找次数是否为0,为0则为真,标记查找次数
    {
        QueryCnt[p]++; 
        return 1;
    }
    else 
    {
        QueryCnt[p]++;//查找次数不为零
        return 2;
    }
}
int main()
{
    int n;
    scanf("%d", &n);
    while(n--)
    {
        scanf("%s", str);
        insert(str);
    }
    int m;
    scanf("%d", &m);
    while(m--)
    {
        scanf("%s", str);
        int k = query(str);
        if(k == 0) puts("WRONG");
        else if(k == 1) puts("OK");
        else puts("REPEAT");
    }
    return 0;
}

P2922 [USACO08DEC]Secret Message G

题目描述
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.

Memory Limit: 32MB

POINTS: 270
贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息.

信息是二进制的,共有M(1≤M≤50000)条.反间谍能力很强的约翰已经部分拦截了这些信息,知道了第i条二进制信息的前bi(l《bi≤10000)位.他同时知道,奶牛使用N(1≤N≤50000)条暗号.但是,他仅仅知道第J条暗号的前cj(1≤cj≤10000)位.

对于每条暗号J,他想知道有多少截得的信息能够和它匹配.也就是说,有多少信息和这条暗号有着相同的前缀.当然,这个前缀长度必须等于暗号和那条信息长度的较小者.

在输入文件中,位的总数(即∑Bi+∑Ci)不会超过500000.
输入格式

  • 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.

输入输出样例
输入 #1复制

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复制

1 
3 
1 
1 
2 

说明/提示
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
解答一

#include<iostream>
#include<cstdio>
using namespace std;
const int N = 500010;
int a[N];
int son[N][2], EndCnt[N], BranchCnt[N], idx;
//EndCnt[N]存储以该节点结尾的字符串数
//BranchCnt[N]存储经过该节点的字符串数(不包括以该节点结尾的字符串数)
void insert(int a[], int n)
{
    int p = 0;
    for(int i = 0; i < n; i++)
    {
        int u = a[i];
        if(!son[p][u]) son[p][u] = ++idx;
        p = son[p][u];
        if(i < n - 1) BranchCnt[p]++;//除了最后一个字符,上面的每一个节点,经过这个字符串的数都+1
    }
    EndCnt[p]++;//字符串读完,标记结尾的字符
}
int query(int a[], int n)
{
    int p = 0, res = 0, i;
    for( i = 0; i < n; i++)
    {
        int u = a[i];
        if(!son[p][u]) break;//中途不匹配
        p = son[p][u];
        if(EndCnt[p]) res += EndCnt[p];//信息为前缀
    }
    if(i >= n) res += BranchCnt[p]; //暗号为前缀
    return res;
}
int main()
{
    int m, n, a[10010];
    scanf("%d%d", &m, &n);
    int x;
    for(int i =  0; i < m; i++)
    {
        scanf("%d", &x);
        for(int j = 0; j < x; j++) scanf("%d", &a[j]);
        insert(a, x);
    }
    for(int i = 0; i < n ; i++)
    {
        scanf("%d", &x);
        for(int j = 0; j < x; j++) scanf("%d", &a[j]);
        printf("%d\n", query(a,x));
    }
    return 0;
}

解答二

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 500020;
char s[N];
int n,m,p,idx;
int cnt[N],sum[N],son[N][3];//sum代表有sum个单词经过这个节点;cnt代表有cnt个单词在这个节点终结
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        int b;
        p=0;
        scanf("%d",&b);
        for(int i=1;i<=b;i++)  scanf("%c",&s[0]),scanf("%c",&s[i]);
        for(int i=1;i<=b;i++)
        {
            int x=s[i]-'0';
            if(!son[p][x])
                son[p][x]=++idx;
            sum[son[p][x]]++;
            p=son[p][x];
        }
        cnt[p]++;
    }
    for(int i=1;i<=m;i++)
    {
        int b,flag=0,ans=0;p=0;
        scanf("%d",&b);
        for(int i=1;i<=b;i++)    scanf("%c",&s[0]),scanf("%c",&s[i]);
        for(int i=1;i<=b;i++)
        {
            int x=s[i]-'0';
            if(!son[p][x])
            {
                flag=1;
                break;
            }
            p=son[p][x];
            ans+=cnt[p];
        }
        if(!flag)    cout<<sum[p]+ans-cnt[p]<<endl;
        else cout<<ans<<endl;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值