ALDS1_4_A-LinearSearch.
Description:

You are given a sequence of n integers S and a sequence of different q integers T. Write a program which outputs C, the number of integers in T which are also in the set S.

Input:

In the first line n is given. In the second line, n integers are given. In the third line q is given. Then, in the fourth line, q integers are given.

Output:

Print C in a line.

Constraints:

n ≤ 10000

q ≤ 500

0 ≤ an element in S ≤ 109

0 ≤ an element in T ≤ 109

SampleInput1:

5

1 2 3 4 5

3

3 4 1

SampleOutput1:

3

SampleInput2:

3

3 1 2

1

5

SampleOutput2:

0

Codes:
//#define LOCAL

#include <cstdio>

int search(int A[], int n, int key) {
int i = 0; A[n] = key;
while(A[i] != key) ++i;
return i!=n;
} int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int i, n, q, key, sum = 0, A[10010];
scanf("%d", &n);
for(i=0; i<n; ++i) scanf("%d", &A[i]);
scanf("%d", &q);
for(i=0; i<q; ++i) {
scanf("%d", &key);
if(search(A, n, key)) ++sum;
}
printf("%d\n", sum); return 0;
}

ALDS1_4_B-BinarySearch.

Description:

You are given a sequence of n integers S and a sequence of different q integers T. Write a program which outputs C, the number of integers in T which are also in the set S.

Input:

In the first line n is given. In the second line, n integers are given. In the third line q is given. Then, in the fourth line, q integers are given.

Output:

Print C in a line.

Constraints:

Elements in S is sorted in ascending order

n ≤ 100000

q ≤ 50000

0 ≤ an element in S ≤ 109

0 ≤ an element in T ≤ 109

SampleInput1:

5

1 2 3 4 5

3

3 4 1

SampleOutput1:

3

SampleInput2:

3

1 2 3

1

5

SampleOutput2:

0

Codes:
//#define LOCAL

#include <cstdio>

int n, A[1000010];

int binarySearch(int key) {
int left = 0, right = n;
while(left < right) {
int mid = (left+right)/2;
if(key > A[mid]) left = mid+1;
else if(key == A[mid]) return 1;
else right = mid;
}
return 0;
} int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int i, q, key, sum = 0;
scanf("%d", &n);
for(i=0; i<n; ++i) scanf("%d", &A[i]);
scanf("%d", &q); for(i=0; i<q; ++i) {
scanf("%d", &key);
if(binarySearch(key)) ++sum;
}
printf("%d\n", sum); return 0;
}

ALDS1_4_C-Dictionary.

Description:

Your task is to write a program of a simple dictionary which implements the following instructions:

insert str: insert a string str in to the dictionary

find str: if the distionary contains str, then print 'yes', otherwise print 'no'

Input:

In the first line n, the number of instructions is given. In the following n lines, n instructions are given in the above mentioned format.

Output:

Print yes or no for each find instruction in a line.

Constraints:

A string consists of 'A', 'C', 'G', or 'T'

1 ≤ length of a string ≤ 12

n ≤ 1000000

SampleInput1:

5

insert A

insert T

insert C

find G

find A

SampleOutput1:

no

yes

SampleInput2:

13

insert AAA

insert AAC

insert AGA

insert AGG

insert TTT

find AAA

find CCC

find CCC

insert CCC

find CCC

insert T

find TTT

find T

SampleOutput2:

yes

no

no

yes

yes

yes

Codes:
//#define LOCAL

#include <cstdio>
#include <cstring> #define M 1046527
#define NIL (-1)
#define L 14
char H[M][L]; int getChar(char ch) {
if(ch == 'A') return 1;
else if(ch == 'C') return 2;
else if(ch == 'G') return 3;
else if(ch == 'T') return 4;
else return 0;
} long long getKey(char str[]) {
int len = strlen(str);
long long sum = 0, p = 1, i;
for(i=0; i<len; ++i) {
sum += p*(getChar(str[i]));
p *= 5;
}
return sum;
} int h1(int key) {return key%M;}
int h2(int key) {return 1+(key%(M-1));} int find(char str[]) {
long long key, i, h;
key = getKey(str);
for(i=0; ; ++i) {
h = (h1(key)+i*h2(key))%M;
if(strcmp(H[h], str) == 0) return 1;
else if(strlen(H[h]) == 0) return 0;
}
return 0;
} int insert(char str[]) {
long long key, i, h;
key = getKey(str);
for(i=0; ; ++i) {
h = (h1(key)+i*h2(key))%M;
if(strcmp(H[h], str) == 0) return 1;
else if(strlen(H[h]) == 0) {
strcpy(H[h], str);
return 0;
}
}
return 0;
} int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int i, n, h;
char str[L], com[9];
for(i=0; i<M; ++i) H[i][0] = '\0';
scanf("%d", &n); for(i=0; i<n; ++i) {
scanf("%s %s", com, str);
if(com[0] == 'i') insert(str);
else {
if(find(str)) printf("yes\n");
else printf("no\n");
}
} return 0;
}

ALDS1_4_D-Allocation.

Codes:
#include <iostream>
using namespace std; #define MAX 100000
typedef long long llong;
int n, k; llong T[MAX]; int check(llong P) {
int i = 0;
for(int j=0; j<k; ++j) {
llong s = 0;
while(s+T[i] <= P) {
s += T[i++];
if(i == n) return n;
}
}
return i;
} int solve() {
llong mid, left = 0, right = 1000000000;
while(right-left > 1) {
mid = (left+right)/2;
int v = check(mid);
if(v >= n) right = mid;
else left = mid;
}
return right;
} int main()
{
cin >> n >> k;
for(int i=0; i<n; ++i) cin >> T[i];
cout << solve() << endl;
}

ALDS1_5_A-ExhaustiveSearch.

Description:

Write a program which reads a sequence A of n elements and an integer M, and outputs "yes" if you can make M by adding elements in A, otherwise "no". You can use an element only once.

You are given the sequence A and q questions where each question contains Mi.

Input:

In the first line n is given. In the second line, n integers are given. In the third line q is given. Then, in the fourth line, q integers (Mi) are given.

Output:

For each question Mi, print yes or no.

Constraints:

n ≤ 20

q ≤ 200

1 ≤ elements in A ≤ 2000

1 ≤ Mi ≤ 2000

SampleInput:

5

1 5 7 10 21

8

2 4 17 8 22 21 100 35

SampleOutput:

no

no

yes

yes

yes

yes

no

no

Codes:
//#define LOCAL

#include <cstdio>

int n, A[50];

int solve(int i, int k) {
if(!k) return 1;
if(i >= n) return 0;
return solve(i+1, k)||solve(i+1, k-A[i]);
} int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int i, q, k;
scanf("%d", &n);
for(i=0; i<n; ++i) scanf("%d", &A[i]);
scanf("%d", &q); for(i=0; i<q; ++i) {
scanf("%d", &k);
if(solve(0, k)) printf("yes\n");
else printf("no\n");
} return 0;
}

ALDS1_5_C-KochCurve.

Description:

Write a program which reads an integer n and draws a Koch curve based on recursive calles of depth n.

The Koch curve is well known as a kind of fractals.

You can draw a Koch curve in the following algorithm:

Divide a given segment (p1, p2) into three equal segments.

Replace the middle segment by the two sides of an equilateral triangle (s, u, t) of the same length as the segment.

Repeat this procedure recursively for new segments (p1, s), (s, u), (u, t), (t, p2).

You should start (0, 0), (100, 0) as the first segment.

Input:

An integer n is given.

Output:

Print each point (x, y) of the Koch curve. Print a point in a line. You should start the point(0, 0), which is the endpoint of the first segment and end with the point (100, 0), the other endpoint so that you can draw the Koch curve as an unbroken line. Each solution should be given as a decimal with an arbitrary number of fractional digits, and with an absolute error of at most 10-4.

Constraints:

0 ≤ n ≤ 6

SampleInput1:

1

SampleOutput1:

0.00000000 0.00000000

33.33333333 0.00000000

50.00000000 28.86751346

66.66666667 0.00000000

100.00000000 0.00000000

SampleInput2:

2

SampleOutput2:

0.00000000 0.00000000

11.11111111 0.00000000

16.66666667 9.62250449

22.22222222 0.00000000

33.33333333 0.00000000

38.88888889 9.62250449

33.33333333 19.24500897

44.44444444 19.24500897

50.00000000 28.86751346

55.55555556 19.24500897

66.66666667 19.24500897

61.11111111 9.62250449

66.66666667 0.00000000

77.77777778 0.00000000

83.33333333 9.62250449

88.88888889 0.00000000

100.00000000 0.00000000

Codes:
//#define LOCAL

#include <cstdio>
#include <cmath> struct Point{ double x, y;}; void koch(int n, Point a, Point b) {
if(!n) return ; Point s, t, u;
double th = M_PI*60.0/180.0; s.x = (2.0*a.x+1.0*b.x)/3.0;
s.y = (2.0*a.y+1.0*b.y)/3.0;
t.x = (1.0*a.x+2.0*b.x)/3.0;
t.y = (1.0*a.y+2.0*b.y)/3.0;
u.x = (t.x-s.x)*cos(th)-(t.y-s.y)*sin(th)+s.x;
u.y = (t.x-s.x)*sin(th)+(t.y-s.y)*cos(th)+s.y; koch(n-1, a, s);
printf("%.8f %.8f\n", s.x, s.y);
koch(n-1, s, u);
printf("%.8f %.8f\n", u.x, u.y);
koch(n-1, u, t);
printf("%.8f %.8f\n", t.x, t.y);
koch(n-1, t, b);
} int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int n; Point a, b;
scanf("%d", &n);
a.x = 0, a.y = 0, b.x = 100, b.y = 0; printf("%.8f %.8f\n", a.x, a.y);
koch(n, a, b);
printf("%.8f %.8f\n", b.x, b.y); return 0;
}

AOJ/搜索与递归及分治法习题集的更多相关文章

  1. AOJ/搜索递归分治法习题集

    ALDS1_4_A-LinearSearch. Description: You are given a sequence of n integers S and a sequence of diff ...

  2. Leetcode Lect4 二叉树中的分治法与遍历法

    在这一章节的学习中,我们将要学习一个数据结构——二叉树(Binary Tree),和基于二叉树上的搜索算法. 在二叉树的搜索中,我们主要使用了分治法(Divide Conquer)来解决大部分的问题. ...

  3. 分治法避免定义多个递归函数,应该使用ResultType

    总结:对二叉树应用分治法时,应避免定义多个递归函数,当出现需要递归求解多种的结果时,尽量使用ResultType来让一次递归返回多种结果. 题目:Binary Tree Maximum Path Su ...

  4. ACM/ICPC 之 分治法入门(画图模拟:POJ 2083)

    题意:大致就是要求画出这个有规律的Fractal图形了= = 例如 1 对应 X 2 对应 X  X   X    X  X 这个题是个理解分治法很典型的例子(详情请参见Code) 分治法:不断缩小规 ...

  5. 分治法(一)(zt)

    这篇文章将讨论: 1) 分治策略的思想和理论 2) 几个分治策略的例子:合并排序,快速排序,折半查找,二叉遍历树及其相关特性. 说明:这几个例子在前面都写过了,这里又拿出来,从算法设计的策略的角度把它 ...

  6. C语言实现快速排序法(分治法)

    title: 快速排序法(quick sort) tags: 分治法(divide and conquer method) grammar_cjkRuby: true --- 算法原理 分治法的基本思 ...

  7. p1257 平面上最接近点对---(分治法)

    首先就是一维最接近点的情况... #include<iostream> #include<cstdio> #include<cstring> #include< ...

  8. 分治法——归并排序(mergesort)

    首先上代码. #include <iostream> using namespace std; int arr[11]; /*两个序列合并成一个序列.一共三个序列,所以用 3 根指针来处理 ...

  9. python 实现分治法的几个例子

    分治法所能解决的问题一般具有以下几个特征: 1) 该问题的规模缩小到一定的程度就可以容易地解决 2) 该问题可以分解为若干个规模较小的相同问题,即该问题具有最优子结构性质. 3) 利用该问题分解出的子 ...

随机推荐

  1. C# wpf中关于binding的converter无效的情况

    最近碰到bingding设置了convert转换无效的问题.困扰了我好久.这里记录分析一下. 先说下现象 我把TextBox的text属性  绑定到了对应的 convert.代码如下 希望吧pd_no ...

  2. java基本数据类型和包装类之间的转换(装箱,拆箱)

    1.装箱:把基本数据类型转换成包装类 1.1自动装箱 int t1=2; Integer t2 =t1; 1.2手动装箱 Integer t3 = new Integer(t1); 2.拆箱:把包装类 ...

  3. 网络流量预测入门(三)之LSTM预测网络流量

    目录 网络流量预测入门(三)之LSTM预测网络流量 数据集介绍 预测流程 数据集准备 SVR预测 LSTM 预测 优化点 网络流量预测入门(三)之LSTM预测网络流量 在上篇博客LSTM机器学习生成音 ...

  4. Hutool中那些常用的工具类和方法

    Hutool中那些常用的工具类和方法 Hutool是一个Java工具包,它帮助我们简化每一行代码,避免重复造轮子.如果你有需要用到某些工具方法的时候,不妨在Hutool里面找找,可能就有.本文将对Hu ...

  5. tensorflow1.12 queue 笔记

    主要参考:https://www.tensorflow.org/api_guides/python/threading_and_queues#Queue_usage_overview 自动方式 For ...

  6. PS-头发丝抠图

    [PS版本]Photoshop CS5 [主题]头发丝抠图 [操作步骤] 第一步:打开待处理图片,复制图层: 第二步:快速选择工具选择主体(也可用魔术棒用具选择背景色,然后反向): 第三步:点击&qu ...

  7. win10画板超实用的快捷键

    win10画板超实用的快捷键链接: Windows 7 画图中的快捷键 Windows中画图的快捷键 其中有windows默认的快捷键,关于画图工具加入到快捷工具也有详细的介绍.

  8. 剑指 Offer 32 - III. 从上到下打印二叉树 III

    剑指 Offer 32 - III. 从上到下打印二叉树 III 请实现一个函数按照之字形顺序打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右到左的顺序打印,第三行再按照从左到右的顺序打印, ...

  9. 八、Abp vNext 基础篇丨标签聚合功能

    介绍 本章节先来把上一章漏掉的上传文件处理下,然后实现Tag功能. 上传文件 上传文件其实不含在任何一个聚合中,它属于一个独立的辅助性功能,先把抽象接口定义一下,在Bcvp.Blog.Core.App ...

  10. Element 对话框简单使用

    官方文档介绍的是页内对话框,但没有基于组件的对话框,这里记录一下,原理就是父子传值是否显示 父页导入组件 <template> <div class="home" ...