3039——迷之好奇

迷之好奇
Time Limit: 2000 ms Memory Limit: 65536 KiB
Submit Statistic
Problem Description

FF得到了一个有n个数字的集合。不要问我为什么,有钱,任性。
FF很好奇的想知道,对于数字x,集合中有多少个数字可以在x前面添加任意数字得到。
如,x = 123,则在x前面添加数字可以得到4123,5123等。
Input

多组输入。
对于每组数据
首先输入n(1<= n <= 100000)。
接下来n行。每行一个数字y(1 <= y <= 100000)代表集合中的元素。
接下来一行输入m(1 <= m <= 100000),代表有m次询问。
接下来的m行。
每行一个正整数x(1 <= x <= 100000)。
Output

对于每组数据,输出一个数字代表答案。
Sample Input

3
12345
66666
12356
3
45
12345
356
Sample Output

1
0
1

**#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
    int data;
    int next[26];
};
struct node a[10000010];
int top;
struct node*creat_kong()
{
    int i;
    struct node*root=&a[top++];
    root->data=0;
    for(i=0; i<10; i++)
    {
        root->next[i]=NULL;
    }
    return root;
}
struct node*insert(struct node*root,char str[])
{
    struct node*p=root;
    int len=strlen(str);
    int i,t;
    for(i=len-1; i>=0; i--)
    {
        t=str[i]-'0';
        if(!p->next[t])
        {
            p->next[t]=creat_kong();
        }
        p=p->next[t];
        p->data++;
    }
    p->data--;
    return root;
}
int find(struct node*root,char str[])
{
    struct node*p=root;
    int len=strlen(str);
    int i,t;
    for(i=len-1; i>=0; i--)
    {
        t=str[i]-'0';
        if(!p->next[t])
        {
            return 0;
        }
        p=p->next[t];
    }
    return p->data;
}
int main()
{
    int n,m,i;
    char str[12];
    while(scanf("%d",&n)!=EOF)
    {
        top=0;
        struct node*root=creat_kong();
        while(n--)
        {
            scanf("%s",str);
            root=insert(root,str);
        }
        scanf("%d",&m);
        while(m--)
        {
            scanf("%s",str);
            printf("%d\n",find(root,str));
        }
    }
    return 0;
}**
**#include <stdio.h>
#include <string.h>
int top;
struct node
{
    int next[26];
    int flag;
} st[5001000];
int creat()
{
    memset(st[top].next,-1,sizeof(st[top].next));
    st[top].flag=0;
    return top++;
}
void insertt(int root,char*s)
{
    int len=strlen(s);
    for(int i=len-1; i>=0; i--)
    {
        int t=s[i]-'0';
        if(st[root].next[t]==-1)
        {
            st[root].next[t]=creat();
        }
        st[root].flag++;
        root=st[root].next[t];
    }
}
int cmp(char *s,int root)
{
    int len=strlen(s);
    for(int i=len-1; i>=0; i--)
    {
        int  t=s[i]-'0';
        if(st[root].next[t]==-1)
        {
            return 0;
        }
        root=st[root].next[t];
    }
    return st[root].flag;
}
int main()
{
    int n,m,root;
    char s[101];
    char s1[101];
    while(~scanf("%d",&n))
    {
        top=0;
        root=creat();
        while(n--)
        {
            scanf("%s",s);
            insertt(root,s);
        }
        scanf("%d",&m);
        while(m--)
        {
            scanf("%s",s1);
            printf("%d\n",cmp(s1,root));
        }
    }
    return 0;
}**
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZ --瑞 hopeACMer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值