Good Bye 2013

u010660276發表於2014-01-04
C. New Year Ratings Change
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int a[300005],p[300005],ans[300005];
int cmp(const int x,const int y)
{
    return a[x]<a[y];
}

int main()
{
    int n,i,j;
    while(scanf("%d",&n)!=EOF)
    {
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            p[i]=i;
        }
        sort(p,p+n,cmp);
        int now=0;
        for(i=0;i<n;i++)
        {
            if(a[p[i]]>=now)
            {
                ans[p[i]]=a[p[i]];
                now=a[p[i]]+1;
            }
            else
            {
                ans[p[i]]=now;
                now++;
            }
        }
        for(i=0;i<n;i++)
            printf("%d ",ans[i]);
        printf("\n");
    }
}
D. New Year Letter

Many countries have such a New Year or Christmas tradition as writing a letter to Santa including a wish list for presents. Vasya is an ordinary programmer boy. Like all ordinary boys, he is going to write the letter to Santa on the New Year Eve (we Russians actually expect Santa for the New Year, not for Christmas).

Vasya has come up with an algorithm he will follow while writing a letter. First he chooses two strings, s1 anf s2, consisting of uppercase English letters. Then the boy makes string sk, using a recurrent equation sn = sn - 2 + sn - 1, operation '+' means a concatenation (that is, the sequential record) of strings in the given order. Then Vasya writes down string sk on a piece of paper, puts it in the envelope and sends in to Santa.

Vasya is absolutely sure that Santa will bring him the best present if the resulting string sk has exactly x occurrences of substring AC (the short-cut reminds him оf accepted problems). Besides, Vasya decided that string s1 should have length n, and string s2 should have length m. Vasya hasn't decided anything else.

At the moment Vasya's got urgent New Year business, so he asks you to choose two strings for him, s1 and s2 in the required manner. Help Vasya.

Input

The first line contains four integers k, x, n, m (3 ≤ k ≤ 50; 0 ≤ x ≤ 109; 1 ≤ n, m ≤ 100).

Output

In the first line print string s1, consisting of n uppercase English letters. In the second line print string s2, consisting of m uppercase English letters. If there are multiple valid strings, print any of them.

If the required pair of strings doesn't exist, print "Happy new year!" without the quotes.

Sample test(s)
Input
3 2 2 2
Output
AC
AC
Input
3 3 2 2
Output
Happy new year!
Input
3 0 2 2
Output
AA
AA
Input
4 3 2 1
Output
Happy new year!
Input
4 2 2 1
Output
Happy new year!

思路:參考別人的程式碼。。。看是看懂了,但感覺自己還真不一定能寫出來。。。暴力列舉,dp,列舉兩個串的頭尾字母和AC個數。每次判斷進行dp遞推。如果i - 2串為A且i - 1串頭為C。dp[i]會多組成一個AC就+1。

下面是程式碼:

#include <stdio.h>
#include <string.h>
const int N = 105;

int k, x, n, m;
__int64 f[N];
char ans[N], s[N], e[N];

void init() {
	scanf("%d%d%d%d", &k, &x, &n, &m);
}

void print(char s, char e, int len, int num) {
	memset(ans, 0, sizeof(ans));
	ans[0] = s; ans[len - 1] = e;
	int i, bo = (s == 'A' ? 0 : 1);
	for (i = 1; i < len - 1; i++)
		ans[i] = 'B';
	for (i = 0; i < num; i++) {
		ans[bo + 2 * i] = 'A';
		ans[bo + 2 * i + 1] = 'C';
	}
	printf("%s\n", ans);
}

bool ISOK(char s1, char e1, char s2, char e2, int xx, int yy) {
	s[1] = s1; e[1] = e1; s[2] = s2; e[2] = e2; f[1] = xx; f[2] = yy;
	for (int i = 3; i <= k; i ++) {
		s[i] = s[i - 2];
		e[i] = e[i - 1];
		f[i] = f[i - 1] + f[i - 2] + (e[i - 2] == 'A' && s[i - 1] == 'C');
	}
	if (f[k] == x)
		return true;
	return false;
}

bool judge() {
	for (char s1 = 'A'; s1 <= 'C'; s1++) {
		for (char e1 = 'A'; e1 <= 'C'; e1++) {
			if (n == 1 && s1 != e1) continue;
			for (char s2 = 'A'; s2 <= 'C'; s2++) {
				for (char e2 = 'A'; e2 <= 'C'; e2++) {
					if (m == 1 && s2 != e2) continue;
					int lx = n - (s1 != 'A') - (e1 != 'C');
					int ly = m - (s2 != 'A') - (e2 != 'C');
					lx /= 2; ly /= 2;
					for (int xx = 0; xx <= lx; xx++)
						for (int yy = 0; yy <= ly; yy++) {
							if (n == 2 && s1 == 'A' && e1 == 'C' && xx == 0) continue;
							if (m == 2 && s2 == 'A' && e2 == 'C' && yy == 0) continue;
							if (ISOK(s1, e1, s2, e2, xx, yy)) {
								print(s1, e1, n, xx);
								print(s2, e2, m, yy);
								return true;
							}
						}
				}
			}
		}
	}
	return false;
}

void solve() {
	if (!judge())
		printf("Happy new year!\n");
}

int main() {
	init();
	solve();
	return 0;
}




相關文章