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", &lt, &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)的更多相关文章

  1. 2016百度之星 资格赛ABCDE

    看题:http://bestcoder.hdu.edu.cn/contests/contest_show.php?cid=690 交题:http://acm.hdu.edu.cn/search.php ...

  2. HDU 5688:2016"百度之星" - 资格赛 Problem D

    原文链接:https://www.dreamwings.cn/hdu5688/2650.html Problem D Time Limit: 2000/1000 MS (Java/Others)    ...

  3. HDU 5686:2016"百度之星" - 资格赛 Problem B

    原文链接:https://www.dreamwings.cn/hdu5686/2645.html Problem B Time Limit: 2000/1000 MS (Java/Others)    ...

  4. HDU 5685:2016"百度之星" - 资格赛 Problem A

    原文链接:https://www.dreamwings.cn/hdu5685/2637.html Problem A Time Limit: 2000/1000 MS (Java/Others)    ...

  5. 2016"百度之星" - 资格赛(Astar Round1)D

    Problem Description 度熊所居住的 D 国,是一个完全尊重人权的国度.以至于这个国家的所有人命名自己的名字都非常奇怪.一个人的名字由若干个字符组成,同样的,这些字符的全排列的结果中的 ...

  6. 2016百度之星资格赛 Round1(2,3,4题)

    Problem B Accepts: 2515 Submissions: 9216 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...

  7. 2016"百度之星" - 资格赛(Astar Round1)

    逆元 1001 Problem A 求前缀哈希和逆元 #include <bits/stdc++.h> typedef long long ll; const int MOD = 9973 ...

  8. 2016"百度之星"-资格赛

    //本题要求:(Ar*A2...An)%p,亦即[(A1*A2*...An)/(A1*A2*...Ar-1)]%p,由于A1*A2...An乘积过大,无法求得相除所得的结果 //我们需要用到乘法逆元( ...

  9. 2016"百度之星" - 资格赛(Astar Round1) 1004

    思路:题目很简单,直接用map记录每个字符串的个数就可以了.记得对每个字符串先sort(). AC代码: #include <cstdio> #include <stdlib.h&g ...

随机推荐

  1. web前端基础知识学习网站推介

    内容:一.基础知识及学习资料1. HTML入门学习:http://www.w3school.com.cn/html/index.aspHTML5 入门学习:http://www.w3school.co ...

  2. Java底层代码实现多文件读取和写入

    需求: "E:/data/"目录下有四个文件夹,如下: 每个文件夹下有几个.csv文件,如下: 将每个文件夹下的.csv文件合并成一个以该文件夹命名的.csv文件. 做法: 找到& ...

  3. C语言中auto,register,static,const,volatile的区别

    1)auto 这个关键字用于声明变量的生存期为自动,即将不在任何类.结构.枚举.联合和函数中定义的变量视为全局变量,而在函数中定义的变量视为局部变量.这个关键字不怎么多写,因为所有的变量默认就是aut ...

  4. Python编程-函数进阶二

    一.生成器补充 1.什么是生成器? 可以理解为一种数据类型,这种数据类型自动实现了迭代器协议(其他的数据类型需要调用自己内置的__iter__方法),所以生成器就是可迭代对象. 2.生成器分类 (1) ...

  5. SQL单行函数和多行函数

    单行函数和多行函数示意图: 单行函数分为五种类型:字符函数.数值函数.日期函数.转换函数.通用函数 单行函数: --大小写控制函数 select lower('Hello World') 转小写, u ...

  6. Unity发布安卓后,安卓输入键盘字体白色

    项目里需要用到显示手机电池电量的,但是又不想写安卓,倒jar包,还要做配置,还要写IOS,好麻烦的说.一查,unity后期版本有这个API,索性就升级高版本的了.但是遇到个小问题,那就是安卓输入的时候 ...

  7. Ubuntu 12.04下安装OpenCV 2.4.2

    http://sourceforge.net/projects/opencvlibrary/files/ Ubuntu 12.04下安装OpenCV 2.4.2 http://blog.csdn.ne ...

  8. CentOS 7 导入epel库

    yum install epel-release 或者到百度云下载相应的 rpm 包进行安装 rpm -ivh epel-release-7-9.noarch.rpm

  9. JSP 导出Excel表格

    ES6语法 传入一个table的id,然后在导出excel按钮上加入一个<a href="#" id="buttonId">导出Excel</ ...

  10. java中的特殊有用类

    1.MessageDigest:类似与md5加密算法应用的功能类