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:
  1. //#define LOCAL
  2. #include <cstdio>
  3. int search(int A[], int n, int key) {
  4. int i = 0; A[n] = key;
  5. while(A[i] != key) ++i;
  6. return i!=n;
  7. }
  8. int main()
  9. {
  10. #ifdef LOCAL
  11. freopen("E:\\Temp\\input.txt", "r", stdin);
  12. freopen("E:\\Temp\\output.txt", "w", stdout);
  13. #endif
  14. int i, n, q, key, sum = 0, A[10010];
  15. scanf("%d", &n);
  16. for(i=0; i<n; ++i) scanf("%d", &A[i]);
  17. scanf("%d", &q);
  18. for(i=0; i<q; ++i) {
  19. scanf("%d", &key);
  20. if(search(A, n, key)) ++sum;
  21. }
  22. printf("%d\n", sum);
  23. return 0;
  24. }

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:
  1. //#define LOCAL
  2. #include <cstdio>
  3. int n, A[1000010];
  4. int binarySearch(int key) {
  5. int left = 0, right = n;
  6. while(left < right) {
  7. int mid = (left+right)/2;
  8. if(key > A[mid]) left = mid+1;
  9. else if(key == A[mid]) return 1;
  10. else right = mid;
  11. }
  12. return 0;
  13. }
  14. int main()
  15. {
  16. #ifdef LOCAL
  17. freopen("E:\\Temp\\input.txt", "r", stdin);
  18. freopen("E:\\Temp\\output.txt", "w", stdout);
  19. #endif
  20. int i, q, key, sum = 0;
  21. scanf("%d", &n);
  22. for(i=0; i<n; ++i) scanf("%d", &A[i]);
  23. scanf("%d", &q);
  24. for(i=0; i<q; ++i) {
  25. scanf("%d", &key);
  26. if(binarySearch(key)) ++sum;
  27. }
  28. printf("%d\n", sum);
  29. return 0;
  30. }

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:
  1. //#define LOCAL
  2. #include <cstdio>
  3. #include <cstring>
  4. #define M 1046527
  5. #define NIL (-1)
  6. #define L 14
  7. char H[M][L];
  8. int getChar(char ch) {
  9. if(ch == 'A') return 1;
  10. else if(ch == 'C') return 2;
  11. else if(ch == 'G') return 3;
  12. else if(ch == 'T') return 4;
  13. else return 0;
  14. }
  15. long long getKey(char str[]) {
  16. int len = strlen(str);
  17. long long sum = 0, p = 1, i;
  18. for(i=0; i<len; ++i) {
  19. sum += p*(getChar(str[i]));
  20. p *= 5;
  21. }
  22. return sum;
  23. }
  24. int h1(int key) {return key%M;}
  25. int h2(int key) {return 1+(key%(M-1));}
  26. int find(char str[]) {
  27. long long key, i, h;
  28. key = getKey(str);
  29. for(i=0; ; ++i) {
  30. h = (h1(key)+i*h2(key))%M;
  31. if(strcmp(H[h], str) == 0) return 1;
  32. else if(strlen(H[h]) == 0) return 0;
  33. }
  34. return 0;
  35. }
  36. int insert(char str[]) {
  37. long long key, i, h;
  38. key = getKey(str);
  39. for(i=0; ; ++i) {
  40. h = (h1(key)+i*h2(key))%M;
  41. if(strcmp(H[h], str) == 0) return 1;
  42. else if(strlen(H[h]) == 0) {
  43. strcpy(H[h], str);
  44. return 0;
  45. }
  46. }
  47. return 0;
  48. }
  49. int main()
  50. {
  51. #ifdef LOCAL
  52. freopen("E:\\Temp\\input.txt", "r", stdin);
  53. freopen("E:\\Temp\\output.txt", "w", stdout);
  54. #endif
  55. int i, n, h;
  56. char str[L], com[9];
  57. for(i=0; i<M; ++i) H[i][0] = '\0';
  58. scanf("%d", &n);
  59. for(i=0; i<n; ++i) {
  60. scanf("%s %s", com, str);
  61. if(com[0] == 'i') insert(str);
  62. else {
  63. if(find(str)) printf("yes\n");
  64. else printf("no\n");
  65. }
  66. }
  67. return 0;
  68. }

ALDS1_4_D-Allocation.

Codes:
  1. #include <iostream>
  2. using namespace std;
  3. #define MAX 100000
  4. typedef long long llong;
  5. int n, k; llong T[MAX];
  6. int check(llong P) {
  7. int i = 0;
  8. for(int j=0; j<k; ++j) {
  9. llong s = 0;
  10. while(s+T[i] <= P) {
  11. s += T[i++];
  12. if(i == n) return n;
  13. }
  14. }
  15. return i;
  16. }
  17. int solve() {
  18. llong mid, left = 0, right = 1000000000;
  19. while(right-left > 1) {
  20. mid = (left+right)/2;
  21. int v = check(mid);
  22. if(v >= n) right = mid;
  23. else left = mid;
  24. }
  25. return right;
  26. }
  27. int main()
  28. {
  29. cin >> n >> k;
  30. for(int i=0; i<n; ++i) cin >> T[i];
  31. cout << solve() << endl;
  32. }

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:
  1. //#define LOCAL
  2. #include <cstdio>
  3. int n, A[50];
  4. int solve(int i, int k) {
  5. if(!k) return 1;
  6. if(i >= n) return 0;
  7. return solve(i+1, k)||solve(i+1, k-A[i]);
  8. }
  9. int main()
  10. {
  11. #ifdef LOCAL
  12. freopen("E:\\Temp\\input.txt", "r", stdin);
  13. freopen("E:\\Temp\\output.txt", "w", stdout);
  14. #endif
  15. int i, q, k;
  16. scanf("%d", &n);
  17. for(i=0; i<n; ++i) scanf("%d", &A[i]);
  18. scanf("%d", &q);
  19. for(i=0; i<q; ++i) {
  20. scanf("%d", &k);
  21. if(solve(0, k)) printf("yes\n");
  22. else printf("no\n");
  23. }
  24. return 0;
  25. }

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:
  1. //#define LOCAL
  2. #include <cstdio>
  3. #include <cmath>
  4. struct Point{ double x, y;};
  5. void koch(int n, Point a, Point b) {
  6. if(!n) return ;
  7. Point s, t, u;
  8. double th = M_PI*60.0/180.0;
  9. s.x = (2.0*a.x+1.0*b.x)/3.0;
  10. s.y = (2.0*a.y+1.0*b.y)/3.0;
  11. t.x = (1.0*a.x+2.0*b.x)/3.0;
  12. t.y = (1.0*a.y+2.0*b.y)/3.0;
  13. u.x = (t.x-s.x)*cos(th)-(t.y-s.y)*sin(th)+s.x;
  14. u.y = (t.x-s.x)*sin(th)+(t.y-s.y)*cos(th)+s.y;
  15. koch(n-1, a, s);
  16. printf("%.8f %.8f\n", s.x, s.y);
  17. koch(n-1, s, u);
  18. printf("%.8f %.8f\n", u.x, u.y);
  19. koch(n-1, u, t);
  20. printf("%.8f %.8f\n", t.x, t.y);
  21. koch(n-1, t, b);
  22. }
  23. int main()
  24. {
  25. #ifdef LOCAL
  26. freopen("E:\\Temp\\input.txt", "r", stdin);
  27. freopen("E:\\Temp\\output.txt", "w", stdout);
  28. #endif
  29. int n; Point a, b;
  30. scanf("%d", &n);
  31. a.x = 0, a.y = 0, b.x = 100, b.y = 0;
  32. printf("%.8f %.8f\n", a.x, a.y);
  33. koch(n, a, b);
  34. printf("%.8f %.8f\n", b.x, b.y);
  35. return 0;
  36. }

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++类对象大小的计算

    (一)常规类大小计算 C++类对象计算需要考虑很多东西,如成员变量大小,内存对齐,是否有虚函数,是否有虚继承等.接下来,我将对此举例说明. 以下内存测试环境为Win7+VS2012,操作系统为32位 ...

  2. 文件File

    前面的话 不能直接访问用户计算机中的文件,一直都是Web应用开发中的一大障碍.2000年以前,处理文件的唯一方式就是在表单中加入<input type="file">字 ...

  3. zip error: Invalid command arguments

    在编译使用svn管理的android代码时,会出现如下错误: zip error: Invalid command arguments (cannot repeat names in zip file ...

  4. 记一次企业级爬虫系统升级改造(六):基于Redis实现免费的IP代理池

    前言: 首先表示抱歉,春节后一直较忙,未及时更新该系列文章. 近期,由于监控的站源越来越多,就偶有站源做了反爬机制,造成我们的SupportYun系统小爬虫服务时常被封IP,不能进行数据采集. 这时候 ...

  5. TCP/IP笔记(三)数据链路层

    数据链路的作用 数据链路层的协议定义了通过通信媒介互连的设备之间传输的规范.通信媒介包括双绞线电缆.同轴电缆.光纤.电波以及红外线等介质.此外,各个设备之间有时也会通过交换机.网桥.中继器等中转数据. ...

  6. 对VC++6.0爱得深沉(二)个性工具的定制

    初始界面看起来很简洁,但是经过一番改造后,你会拥有私人定制的壮丽界面O(∩_∩)O~,让我们开始吧. 1)原有工具的显示隐藏.位置调整: 比如你觉得这个插入图标没什么用,想把这个隐藏,那么你可以打开[ ...

  7. C#实现无边框窗体点击任务栏图标正常最小化和还原

    protected override CreateParams CreateParams{ get { const int WS_MINIMIZEBOX = 0x00020000; // Winuse ...

  8. 零基础入门学习UI设计指南

    第一步:认识设计启蒙必备知识 学习一项技能,尤其是已经有一定沉淀并在各行各业有广泛应用的技能,就一定要对它先有充分的认知.在开始正式学习前,你需要花足够的经历去了解和查阅它的起源.发展.应用.未来. ...

  9. 自适应滤波:奇异值分解SVD

    作者:桂. 时间:2017-04-03  19:41:26 链接:http://www.cnblogs.com/xingshansi/p/6661230.html 声明:欢迎被转载,不过记得注明出处哦 ...

  10. new表达式如何创建对象

    new表达式如何创建对象 前言 刚学java时曾一度认为,构造器中this指向是当前类型的对象,当我们调用new表达式时,由父类的构造器生成对象的一部分并初始化,然后再由子类的构造器补充成完整的对象并 ...