zoukankan      html  css  js  c++  java
  • Bzoj1590——Secret Message(Trie)

    <title>Problem 1590. -- [Usaco2008 Dec]Secret Message 秘密信息</title><center><h2>1590: [Usaco2008 Dec]Secret Message 秘密信息</h2><span class="green">Time Limit: </span>5 Sec&nbsp;&nbsp;<span class="green">Memory Limit: </span>32 MB<br><span class="green">Submit: </span>420&nbsp;&nbsp;<span class="green">Solved: </span>278<br>[<a href="submitpage.php?id=1590">Submit</a>][<a href="problemstatus.php?id=1590">Status</a>][<a href="bbs.php?id=1590">Discuss</a>]</center><h2>Description</h2><div class="content"><div><span style="font-size: medium">&nbsp;&nbsp;&nbsp;&nbsp;贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息.</span></div> <div><span style="font-size: medium">&nbsp;&nbsp;&nbsp;&nbsp;信息是二进制的,共有M(1≤M≤50000)条.反间谍能力很强的约翰已经部分拦截了这些</span><span style="font-size: medium">信息,知道了第i条二进制信息的前bi(l《bi≤10000)位.他同时知道,奶牛使用N(1≤N≤</span><span style="font-size: medium">50000)条密码.但是,他仅仅了解第J条密码的前cj(1≤cj≤10000)位.</span></div> <div><span style="font-size: medium">&nbsp;&nbsp;&nbsp;&nbsp;对于每条密码J,他想知道有多少截得的信息能够和它匹配.也就是说,有多少信息和这条密码有着相同的前缀.当然,这个前缀长度必须等于密码和那条信息长度的较小者.</span></div> <div><span style="font-size: medium">&nbsp;&nbsp;&nbsp;&nbsp;在输入文件中,位的总数(即∑Bi+∑Ci)不会超过500000.</span></div></div><h2>Input</h2><div class="content"><div><span style="font-size: medium">&nbsp;&nbsp;&nbsp;&nbsp;第1行输入N和M,之后N行描述秘密信息,之后M行描述密码.每行先输入一个整数表示信息或密码的长度,之后输入这个信息或密码.所有数字之间都用空格隔开.</span></div></div><h2>Output</h2><div class="content"><div>&nbsp;</div> <div><span style="font-size: medium">&nbsp;&nbsp;&nbsp;&nbsp;共M行,输出每条密码的匹配信息数.</span></div></div><h2>Sample Input</h2> 3 0 1 0<br> 1 1<br> 3 1 0 0<br> 3 1 1 0<br> 1 0<br> 1 1<br> 2 0 1<br> 5 0 1 0 0 1<br> 2 1 1<br> <br> INPUT DETAILS:<br> <br> Four messages; five codewords.<br> The intercepted messages start with 010, 1, 100, and 110.<br> The possible codewords start with 0, 1, 01, 01001, and 11.<br> <br> <br> </span></div><h2>Sample Output</h2> <div class="content"><span class="sampledata">1<br> 3<br> 1<br> 1<br> 2<br> <br> 这道题一开始题目看错,搞得我纠结了45min多o(╥﹏╥)o其实很简单啦 ### 题解 用秘密信息来建一颗Trie树: 对于每个密码我们可以知道在Trie树上它能覆盖多少条信息或者被多少条信息覆盖,需要维护这两个量 然后需要记录每个结点是多少个字符串的尾结点,同时把每次路径上所有点的覆盖标记加1。 查询的时候就可以把路径上所有尾结点标记之和加起来然后再把最后访问到的节点的覆盖值加上就可以了 ``` c++ #include<cstdio> #include<iostream> #include<cmath> #include<cstring> #include<algorithm> #include<cstdlib> using namespace std; int n,m,t,x,ch[1000005][5],tot=1,c[1000005],bo[1000005],f[1000005],ans; int main() { scanf("%d%d",&n,&m); for(int i=1; i<=n; i++){ scanf("%d",&t); int u=1; for(int j=1; j<=t; j++){ scanf("%d",&x); if(!ch[u][x]) ch[u][x]=++tot; u=ch[u][x]; c[u]++; } bo[u]++; } for(int i=1; i<=m; i++){ scanf("%d",&t); for(int i=1; i<=t; i++) scanf("%d",&f[i]); int u=1,ans=0; bool flag=false; for(int i=1; i<=t; i++){ if(!ch[u][f[i]]){ flag=true; break; } u=ch[u][f[i]]; ans+=bo[u]; } if(!flag) ans+=c[u]-bo[u]; printf("%d ",ans); } } ``` miao~~~
  • 相关阅读:
    day4 流程控制while 判断if
    作业2
    C语言I博客作业02
    ActionScript3与PHP的通信
    WordPress代码和分析从主题开始
    事件、委托、异步
    201920201学期 20192405《网络空间安全专业导论》第二周学习总结
    201920201学期 20192405《网络空间安全专业导论》第二周学习总结
    201920201学期 20192405《网络空间安全专业导论》第四周学习总结
    201920201学期 20192405《网络空间安全专业导论》第一周学习总结
  • 原文地址:https://www.cnblogs.com/wangyh1008/p/9403295.html
Copyright © 2011-2022 走看看