UVa 11849 - CD

题目:给你两个有序序列(每一个序列中元素不同),求两序列中都出现的元素个数。

分析:简单题。

合并排序合并过程。

设置两个指针。指向两序列当前元素。那个元素小指针向后移动。相同大则计数加一,同一时候后移。

说明:简单题。(⊙_⊙)

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

int Jack[1000001];
int Jill[1000001];

int main()
{
	int n,m;
	while (~scanf("%d%d",&n,&m) && n+m) {
		for (int i = 0 ; i < n ; ++ i)
			scanf("%d",&Jack[i]);
		for (int i = 0 ; i < m ; ++ i)
			scanf("%d",&Jill[i]);

		int p = 0,q = 0,same = 0;
		while (p < n && q < m)
			if (p < n && q < m && Jack[p] == Jill[q]) {
				same ++;
				p ++;
				q ++;
			}else if (q == m || (p < n && Jack[p] < Jill[q]))
				p ++;
			else if (p == n || (q < m && Jack[p] > Jill[q]))
				q ++;

		printf("%d\n",same);
	}
    return 0;
}
时间: 2024-10-07 05:06:38

UVa 11849 - CD的相关文章

uva 624 CD (01背包)

uva 624 CD You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is on CDs. You need to have it on tapes so the problem to solve is: you have a tape N minutes long. How to choose tracks from CD to get most ou

UVA 624 CD 记录路径DP

开一个数组p 若dp[i-1][j]<dp[i-1][j-a[i]]+a[i]时就记录下p[j]=a[i];表示此时放进一个轨道 递归输出p #include <stdio.h> #include <string.h> #include <stdlib.h> #include <limits.h> #include <malloc.h> #include <ctype.h> #include <math.h> #in

UVA 624 CD

CD Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 62464-bit integer IO format: %lld      Java class name: Main You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music

UVA 624 CD (01背包+打印路径 或 dfs+记录路径)

Description You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is on CDs. You need to have it on tapes so the problem to solve is: you have a tape N minutes long. How to choose tracks from CD to get most o

UVA 624 CD (01背包 带路径)

题意 输入两个数 len,n 表示长度和个数,接下来输入n个数, 表示每一个的长度, 求这n个数能够组成的不超过len的最大长度,并输出这些数. 分析:01背包,dp数组非0表示可以组成的数,dp数组用来记录路径 #include <iostream> #include <queue> #include <cstdio> #include <cstring> #include <cstdlib> #include <stack> #i

【DP】UVA 624 CD 记录路径

开一个数组p 若dp[i-1][j]<dp[i-1][j-a[i]]+a[i]时就记录下p[j]=a[i];表示此时放进一个轨道 递归输出p #include <stdio.h> #include <string.h> #include <stdlib.h> #include <limits.h> #include <malloc.h> #include <ctype.h> #include <math.h> #in

UVA 624 CD (01背包)

//路径记录方法:若是dp[j-value[i]]+value[i]>dp[j]说明拿了这个东西,标志为1, //for循环标志,发现是1,就打印出来,并把背包的容量减少,再在次容量中寻找标志: #include <iostream> #include <cstring> #include <algorithm> using namespace std; int value[30],dp[10001],s[30][10001]; int main() { int

uva 624 CD 01背包打印路径

// 集训最终開始了.来到水题先 #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; int a[23]; int d[23][100000]; int flag[23]; int W,n; void init(){ cin >> n; for (int i=1;i<=n;i++) cin >

小白书关于动态规划

10192 最长公共子序列 http://uva.onlinejudge.org/index.php?option=com_onlinejudge& Itemid=8&page=show_problem&category=114&problem=1133&mosmsg= Submission+received+with+ID+13297616 */ #include <cstdio> #include <string.h> #include&