ACM学习历程—2016"百度之星" - 资格赛(Astar Round1)
http://bestcoder.hdu.edu.cn/contests/contest_show.php?cid=690
A题:
给定字符串,求任意区间的Hash值。
根据题目给定的Hash方式,属于乘法类型,那么就可以预处理出所有的乘法前缀,然后利用逆元,就可以得到任意区间的Hash值。
不过在这题上跪了好久,最后讨论版的一位大神给出了一个很叼的隐含条件:“同志们,当a, b超范围的时候,输出上一次询问的答案,亲测可行。。。太坑了。。。”
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <vector>
#define MOD 9973 using namespace std; const int maxN = ;
int q, len, ans, inv[], s[maxN];
char str[maxN]; void init()
{
//***预处理所有i在质数MOD下的逆元
inv[] = ;
for (int i = ; i < ; i++)
inv[i] = inv[MOD%i]*(MOD-MOD/i) % MOD;
} void input()
{
scanf("%s", str);
len = strlen(str);
for (int i = ; i < len; ++i)
{
if (i == ) s[i] = (str[i]-+MOD)%MOD;
else s[i] = s[i-]*(str[i]-+MOD)%MOD;
}
} bool judge(int lt, int rt)
{
if (lt > rt) return false;
if (lt < || lt > len) return false;
if (rt < || rt > len) return false;
return true;
} inline int getHash(int from, int to)
{
return (from == ? s[to] : s[to]*inv[s[from-]]%MOD);
} void work()
{
int lt, rt;
for (int i = ; i < q; ++i)
{
scanf("%d%d", <, &rt);
if (judge(lt, rt))
ans = getHash(lt-, rt-);
printf("%d\n", ans);
}
} int main()
{
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
init();
while (scanf("%d", &q) != EOF)
{
input();
work();
}
return ;
}
B题:
一个递推,如果有n个1,那么要么前两个1结合,要么不结合,那么p(n) = p(n-1)+p(n-2)。然后Java大数直接打表。
代码:
import java.math.BigInteger;
import java.util.Scanner; public class Main
{
BigInteger p[] = new BigInteger[205]; void init()
{
p[1] = new BigInteger("1");
p[2] = new BigInteger("2");
for (int i = 3; i < 205; ++i)
p[i] = p[i-1].add(p[i-2]);
} public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Main ans = new Main();
int n = 0;
ans.init();
while (input.hasNext())
{
n = input.nextInt();
System.out.println(ans.p[n]);
}
}
}
C题:
一道字符串前缀问题。
有三种操作,插入一个单词,删除给定前缀的所有单词,查询是否存在给定前缀的单词。
用字典树搞,插入的时候,给路径上所有结点的vis加1。
删除的时候,删除前缀后面的所有子树,并将路径上所有结点的vis值,减去最末结点的vis值。
查询时,一旦路径上有一个vis值为0,就查询失败。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <vector> using namespace std; struct Trie
{
int vis;
int next[];
}tree[]; int q, top; void build()
{
top = ;
for (int i = ; i < ; ++i) tree[].next[i] = -;
tree[].vis = ;
} int createNode()
{
top++;
for (int i = ; i < ; ++i) tree[top].next[i] = -;
tree[top].vis = ;
return top;
} void add(char str[])
{
int len = strlen(str), k, now = ;
for (int i = ; i < len; ++i)
{
k = str[i]-'a';
if (tree[now].next[k] == -) tree[now].next[k] = createNode();
now = tree[now].next[k];
tree[now].vis++;
}
} void del(char str[])
{
int len = strlen(str), k, now = , cnt;
for (int i = ; i < len; ++i)
{
k = str[i]-'a';
if (tree[now].next[k] == -) return;
now = tree[now].next[k];
}
for (int i = ; i < ; ++i) tree[now].next[i] = -;
cnt = tree[now].vis;
now = ;
for (int i = ; i < len; ++i)
{
k = str[i]-'a';
now = tree[now].next[k];
tree[now].vis -= cnt;
}
} bool get(char str[])
{
int len = strlen(str), k, now = ;
for (int i = ; i < len; ++i)
{
k = str[i]-'a';
if (tree[now].next[k] == -) return false;
now = tree[now].next[k];
if (!tree[now].vis) return false;
}
return true;
} void work()
{
build();
char op[], str[];
for (int i = ; i < q; ++i)
{
scanf("%s%s", op, str);
switch (op[])
{
case 'i':
add(str);
break;
case 'd':
del(str);
break;
case 's':
if (get(str)) printf("Yes\n");
else printf("No\n");
break;
}
}
} int main()
{
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
while (scanf("%d", &q) != EOF)
work();
return ;
}
D题:
这题直接排序+map能过。没试字典树。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <vector> using namespace std; int main()
{
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
int n;
char str[];
while (scanf("%d", &n) != EOF)
{
map<string, int> s;
for (int i = ; i < n; ++i)
{
scanf("%s", str);
sort(str, str+strlen(str));
printf("%d\n", s[str]++);
}
}
return ;
}
ACM学习历程—2016"百度之星" - 资格赛(Astar Round1)的更多相关文章
- 2016百度之星 资格赛ABCDE
看题:http://bestcoder.hdu.edu.cn/contests/contest_show.php?cid=690 交题:http://acm.hdu.edu.cn/search.php ...
- HDU 5688:2016"百度之星" - 资格赛 Problem D
原文链接:https://www.dreamwings.cn/hdu5688/2650.html Problem D Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 5686:2016"百度之星" - 资格赛 Problem B
原文链接:https://www.dreamwings.cn/hdu5686/2645.html Problem B Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 5685:2016"百度之星" - 资格赛 Problem A
原文链接:https://www.dreamwings.cn/hdu5685/2637.html Problem A Time Limit: 2000/1000 MS (Java/Others) ...
- 2016"百度之星" - 资格赛(Astar Round1)D
Problem Description 度熊所居住的 D 国,是一个完全尊重人权的国度.以至于这个国家的所有人命名自己的名字都非常奇怪.一个人的名字由若干个字符组成,同样的,这些字符的全排列的结果中的 ...
- 2016百度之星资格赛 Round1(2,3,4题)
Problem B Accepts: 2515 Submissions: 9216 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- 2016"百度之星" - 资格赛(Astar Round1)
逆元 1001 Problem A 求前缀哈希和逆元 #include <bits/stdc++.h> typedef long long ll; const int MOD = 9973 ...
- 2016"百度之星"-资格赛
//本题要求:(Ar*A2...An)%p,亦即[(A1*A2*...An)/(A1*A2*...Ar-1)]%p,由于A1*A2...An乘积过大,无法求得相除所得的结果 //我们需要用到乘法逆元( ...
- 2016"百度之星" - 资格赛(Astar Round1) 1004
思路:题目很简单,直接用map记录每个字符串的个数就可以了.记得对每个字符串先sort(). AC代码: #include <cstdio> #include <stdlib.h&g ...
随机推荐
- jdbc驱动jar导入eclipse
在使用JDBC编程时需要连接数据库,导入JAR包是必须的,导入其它的jar包方法同样如此,导入的方法是 打开eclipse 1.右击要导入jar包的项目,点properties 2.左边选择java ...
- mysql ERROR 1045 (28000): Access denied (using password: YES)
mysql 安装完成后 mysql -u root -p #让输入密码直接回车就能登录 设置mysql的root用户初始密码: mysqladmin -u root password 'root' 解 ...
- Python自然语言处理 - 系列三
有监督分类过程 ![enter image description here][1]例子:涉及一个特征器,给定一个姓名分析出是男性名字还是女性名字 分析:男性和女性的名字有一些鲜明的特点.以a,e 和 ...
- 20145230《java程序设计》第五次实验报告
20145230实验五 Java网络编程及安全 实验内容 掌握Socket程序的编写: 掌握密码技术的使用: 设计安全传输系统. 实验步骤 本次实验我负责编写客户端代码的编写,以下是我实验进行的步骤: ...
- One 的使用(1)
方法一:使用命令提示符 第一步:打开d盘 C:Users\dcf>d; 第二步:打开工作空间 D:\>Cd workspace 第三步:打开the one D:\workspace& ...
- java格式化输出 printf 例子
import java.util.Date; public class Printf { public static void main(String[] args) { // %s表示输出字符串,也 ...
- window.onload=function(){}和$(function(){})的区别
1.执行的个数的不同: window.onload()只会执行最后一个,些多个也会被最后一个覆盖. $(function(){})可以写多个,也会执行多个,按照从上至下的顺讯执行 2.执行时间上的不同 ...
- Spring初学之FactoryBean配置Bean
实体bean: Car.java: package spring.beans.factorybean; public class Car { private String name; private ...
- js职责链模式
职责链模式(Chain of Responsiblity),使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系.将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为 ...
- ajax_基础
ajax 请求过程 1.准备发送请求 2.填写请求地址和数据 3.请请求到服务器 4.等待服务器处理数据. 5.接受服务器返回信息 --------------------------------- ...