您的位置:首页 > 其它

【百度之星 - 2016】

2016-05-15 11:18 501 查看
1001.Problem A
http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=690&pid=1001


  类似倒水原理,但是这里是连乘,对于连续累积S。 x = S[a...b] = S[b] % 9973 / S[a - 1] % 9973。 也就是求

a * x = b %9973 线性方程组的解。EXGCD拓展GCD求解。数论题。

Promble B

  斐波那契数列。可以大整数,可以java。我贴下Java代码,好久没用了。

import java.math.*;
import java.io.*;
import java.text.*;
import java.util.Scanner;

public class Main {
public static void main(String []args){
Scanner cin = new Scanner(System.in);
while(cin.hasNext()){
BigInteger r = cin.nextBigInteger();
BigInteger bigTwo = new BigInteger("2");
BigInteger m = BigInteger.ZERO;
BigInteger n = BigInteger.ZERO;
BigInteger ret = BigInteger.ZERO;
while(m.compareTo(r.divide(bigTwo)) <= 0){
n = r.subtract(m);
BigInteger fz = BigInteger.ONE;
BigInteger fm = BigInteger.ONE;
BigInteger left = (n.subtract(m)).add(BigInteger.ONE);
BigInteger right = n;
while(left.compareTo(right) <= 0){
fz = fz.multiply(left);
left = left.add(BigInteger.ONE);
}
left = m;
while(left.compareTo(BigInteger.ONE) >= 0){
fm = fm.multiply(left);
left = left.subtract(BigInteger.ONE);
}
ret = ret.add(fz.divide(fm));
m = m.add(BigInteger.ONE);
}
System.out.println(ret);
}
}
}


Problem C

  字典树。贴一下自己敲的Trie,下次没准用得上。

#include <iostream>
#include <cmath>
#include <cstring>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

const int Max = 26;
class TrieNode {
public:
int num;
TrieNode *next[Max];
TrieNode() {
memset(next, NULL, sizeof next);
num = 0;
}
};
class Trie {
public:

Trie() {
root = new TrieNode();
}
~Trie(){
free(root);
}
// Inserts a word into the trie.
void insert(char* word) {
TrieNode* p = root;
int len = strlen(word);
for(int i = 0; i < len; i ++){
int id = word[i] - 'a';
if(p->next[id] == NULL)
p->next[id] = new TrieNode();
p = p->next[id];
p->num ++;
}
}
// Returns if the word is in the trie.
TrieNode* search(char* word) {
TrieNode *p = root;
int len = strlen(word);
for(int i = 0; i < len; i ++){
int id = word[i] - 'a';
if(p->next[id] == NULL) return NULL;
else{
p = p->next[id];
}
}
return p;
}

void destoryWord(TrieNode* p)
{
if(!p) return ;
for(int i = 0; i < Max; i ++ ){
destoryWord(p -> next[i]);
}
free(p);
}

void destorySon(TrieNode * p)
{
if(!p) return;
for(int i = 0; i < Max; i ++ ){
destoryWord(p -> next[i]);
p->next[i] = NULL;
}
}

void deleteWord(char* word)
{
TrieNode *cur = search(word);
if(!cur) return ;
int count = cur -> num;
destorySon(cur);
TrieNode *p = root;
int len = strlen(word);
for(int i = 0; i < len; i ++){
int id = word[i] - 'a';
p = p -> next[id];
p -> num -= count;
}
}

private:
TrieNode* root;
};

int main()
{
int n;
Trie trie;
scanf("%d",&n);
getchar();
char op[10], str[50];
while(n -- ){
scanf("%s%s",op, str);
if(op[0] == 'i'){
trie.insert(str);
}else if(op[0] == 'd'){
trie.deleteWord(str);
}else {
TrieNode *rec = trie.search(str);
if(rec && rec -> num > 0) printf("Yes\n");
else printf("No\n");
}
}system("pause");
return 0;
}
/*
11
insert aaaa
insert aaab
insert aab
insert aac
insert ab
search aaa
delete aaa
search aaa
search aaab
search aa
search ab
*/


1004.Problem D

  字符串hash。一开始用Map<string, int>tle。我还以为是string输入效率不行,改成字典树用char* 输入过得,但之后有看到人家用map哈希过得,我也是很郁闷啊。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: