UVA11736_Debugging RAM

本文介绍了一个使用字典树和位运算处理字符串匹配问题的C++程序实例。通过建立字典树来存储字符串,并利用位运算进行快速查找与更新操作。该程序实现了字符串的插入、查找功能,并能对特定字符串进行计数。

题目绝对够水,我就不详细说明了。

直接上代码吧。只是提示一下要用 unsigned long long。

(不知道我不用字典树为什么会超时,肿么搞的)

 

#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 2300
#define ll unsigned long long
using namespace std;

ll tag[50050],next[50050][60],N;
char s[maxn];
ll g[maxn],dis,tot,now;
ll b,v,k,n;
ll f[maxn];

ll getcode(char cc)
{
    if (cc>='a' && cc<='z') return cc-'a';
    return cc-'A'+26;
}

void insert(ll x)
{
    ll cur=0,tep;
    for (ll i=0; s[i]; i++)
    {
        tep=getcode(s[i]);
        if (!next[cur][tep]) next[cur][tep]=++N;
        cur=next[cur][tep];
    }
    tag[cur]=x;
}

ll find()
{
    ll cur=0,tep;
    for (ll i=0; s[i]; i++)
    {
        tep=getcode(s[i]);
        if (next[cur][tep]==0) return 0;
        cur=next[cur][tep];
    }
    return tag[cur];
}

ll count()
{
    ll ans=0;
    for (ll i=0; s[i]; i++)
    {
        ans<<=1;
        if (s[i]=='1') ans++;
    }
    return ans;
}

int main()
{
    while (scanf("%llu%llu",&b,&v)!=EOF)
    {
        dis=(1<<b);
        N=0; tot=0;
        memset(next,0,sizeof next);
        memset(tag,0,sizeof tag);
        memset(f,0,sizeof f);
        memset(g,0,sizeof g);
        for (ll i=1; i<=v; i++)
        {
            scanf("%s",s);
            insert(i);
            scanf("%llu",&g[i]);
            tot+=g[i];
        }
        now=1;
        for (ll i=1; i<=tot; i++)
        {
            scanf("%s",s);
            k=count();
            while (g[now]==0) now++;
            g[now]--;
            f[now]=f[now]*dis+k;
        }
        scanf("%llu",&n);
        while (n--)
        {
            scanf("%s",s);
            k=find();
            printf("%s=",s);
            if (k) printf("%llu",f[k]);
            printf("\n");
        }
    }

    return 0;
}

 

转载于:https://www.cnblogs.com/lochan/p/3422440.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值