poj 3290

这个题做了三个多小时,
前2个小时没用理解到这句话

Given a collection of symbols resulting from throwing a set of dice,

我以为是最优子序列问题,各种dp回溯都过不了,但是人家说a set of dice,就是输入的是一个集合,而不是一个序列。隐含的意思是没有顺序问题,可以乱序构造。

后面一个小时是没用理解到这句话

p, q, r, s, and t are WFFs

我光看到p, q, r, s,没用看到and t,然后一直wa。
读题三小时,真正的解题不到10分钟(不算写dp的时间)。

我把自己蠢到了。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define INF 0x3f3f3f3f
#define IMAX = 2147483646;
#define LINF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define ull unsigned long long
#define uint unsigned int

using namespace::std;

char ss[111];
int num[15], nn[4],len;
char tt[] = { 'p', 'q','r', 's', 't','K', 'E', 'C', 'A', 'N' };
// p, q, r, s, t, K, E, C, A, N;
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9;

int get(char c) {
	switch (c) {
	case 'p':return 0;
	case 'q':return 1;
	case 'r':return 2;
	case 's':return 3;
	case 't':return 4;
	case 'K':return 5;
	case 'E':return 6;
	case 'C':return 7;
	case 'A':return 8;
	case 'N':return 9;
	}
}
int gg(char c) {
	if (get(c) <= 4)return 0;
	else if (get(c) <= 8)return 1;
	else return 2;
}

int main() {
	while (scanf("%s", ss), ss[0] != '0') {
		memset(num, 0, sizeof(num));
		memset(nn, 0, sizeof(nn));
		len = strlen(ss);
		for (int i = 0; i < len; i++) {
			num[get(ss[i])]++;
			nn[gg(ss[i])]++;
		}
		if (nn[0] == 0) {
			printf("no WFF possible\n");
			continue;
		}
		if (nn[1] > nn[0] - 1) nn[1] = nn[0] - 1;
		if (nn[0] - 1 > nn[1]) nn[0] = nn[1] + 1;
		//printf("%d %d %d", nn[0], nn[1], nn[2]);
		while (nn[2]) putchar('N'), nn[2]--;
		for (int i = 8; i >= 0; i--) {
			while (num[i]) {
				if (nn[gg(tt[i])] == 0) 
					break;
				putchar(tt[i]);
				nn[gg(tt[i])]--;
				num[i]--;
			}
		}
		putchar('\n');
	}

	return 0;
}

再贴一下我辛辛苦苦写的dp代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define INF 0x3f3f3f3f
#define IMAX = 2147483646;
#define LINF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define ull unsigned long long
#define uint unsigned int

using namespace::std;


int f[2][111][111], n, d[2][111][111];
char str[111];

int dp(int k,int l, int r) {
	if (f[k][l][r] >= 0)return f[k][l][r];
	if (l > r)return 0;
	if (l == r) {
		if (k == 0) 
			if (str[l] == 'q' || str[l] == 'p' || str[l] == 'r' || str[l] == 's') 
				return f[k][l][r] = 1;
			else return f[k][l][r] = 0;
		else return f[k][l][r] = 0;
	}

	int ans = 0, itr = 0;
	if (k == 0) {
		for (int i = l; i < r; i++) {
			if (str[i] == 'N') 
				if (dp(0, i + 1, r)!=0 && dp(0, i + 1, r) + 1 > ans) {
					ans = dp(0, i + 1, r) + 1;
					itr = i;
				}
				
			if (str[i] == 'K' || str[i] == 'A' || str[i] == 'E' || str[i] == 'C')
				if (dp(1, i + 1, r) !=0&&dp(1, i + 1, r) + 1 > ans) {
					ans = dp(1, i + 1, r) + 1;
					itr = i;
				}	
		}
	}
	else {
		for (int i = l; i <= r - 1; i++)
			if (dp(0, l, i) != 0 && dp(0, i + 1, r) != 0 && dp(0, l, i) + dp(0, i + 1, r) > ans) {
				ans = dp(0, l, i) + dp(0, i + 1, r);
				itr = i;
			}
	}
	d[k][l][r] = itr;
	return f[k][l][r] = ans;
}

void print(int k, int l, int r) {
	if (l == r) {
		if (k == 0 && str[l] == 'q' || str[l] == 'p' || str[l] == 'r' || str[l] == 's')
			putchar(str[l]);
		return;
	}
	if (k == 0) {
		putchar(str[d[k][l][r]]);
		if (str[d[k][l][r]] == 'N')print(0, d[k][l][r] + 1, r);
		else print(1, d[k][l][r] + 1, r);
	}
	else {
		print(0, l, d[k][l][r]);
		print(0, d[k][l][r] + 1, r);
	}
}

int main() {
	while (scanf("%s", str+1),str[1]!='0') {
		n = strlen(str + 1);
		memset(f, -1, sizeof(f));
		if (dp(0, 1, n) == 0)printf("no WFF possible\n");
		else print(0, 1, n), printf("\n");
	}

	return 0;
}

你可能感兴趣的:(算法,好题)