UVA 10340 All in All 子序列判定 贪心

题意:给出两个字符串,问第一个字符串是否是第二个的子序列。(不一定需要连续的字串)

只要每次从左向右寻找匹配字符,找到父序列的最右匹配字符的字串,然后从它的下个位置继续找下一个匹配。如果找完父串还没找到就表示不是子序列,如果找到就退出。

代码:

 /*
 *   Author:        illuz 
 *   Blog:          http://blog.csdn.net/hcbbt
 *   File:          uva10057.cpp
 *   Lauguage:      C/C++
 *   Create Date:   2013-08-25 11:30:04
 *   Descripton:    UVA 10057 A mid-summer night's dream, simulation 
 */
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repu(i, a, b) for (int i = (a); i < (b); i++)
#define repf(i, a, b) for (int i = (a); i <= (b); i++)
#define repd(i, a, b) for (int i = (a); i >= (b); i--)
#define swap(a, b) {int t = a; a = b; b = t;}
#define mc(a) memset(a, 0, sizeof(a))
#define ms(a, i) memset(a, i, sizeof(a))
#define sqr(x) ((x) * (x))
#define FI(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
typedef long long LL;
typedef unsigned long long ULL;

/****** TEMPLATE ENDS ******/

const int MAXN = 1000000;
int n, num[MAXN], n1, n2;

int main() {
	while (scanf("%d", &n) != EOF) {
		rep(i, n) {
			scanf("%d", &num[i]);
		}
		sort (num, num + n);
		int k = (n - 1) / 2;
		n1 = 0; n2 = 1;
		if (n % 2) {
			for (int i = k; i >= 0 && num[k] == num[i]; i--)
				n1++;
			for (int i = k + 1; i < n && num[k] == num[i]; i++)
				n1++;
		}
		else {
			for (int i = k; i >= 0 && num[k] == num[i]; i--)
				n1++;
			for (int i = k + 1; i < n && num[k + 1] == num[i]; i++)
				n1++;
			n2 = num[k + 1] - num[k] + 1;
		}
		printf("%d %d %d\n", num[k], n1, n2);
	}
	return 0;
}


你可能感兴趣的:(=====算法相关=====,+基础算法)