CodeForces - 379D New Year Tree Decorations(暴力枚举)

题目链接:http://codeforces.com/problemset/problem/379/D
题意:给出四个数 k , x , n , m k,x,n,m k,x,n,m,构造出长度分别为 n , m n,m n,m的字符串 s 1 s_1 s1, s 2 s_2 s2, s i = s i − 1 + s i − 2 ( i > 2 ) s_i=s_{i-1}+s_{i-2}(i>2) si=si1+si2(i>2),使 s k s_k sk " A C " "AC" "AC"的数量为 x x x,若无法实现输出 H a p p y Happy Happy n e w new new y e a r ! year! year!;

D. New Year Letter

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

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.

Examples
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!

解题思路:暴力枚举;
具体做法:用 i , j i,j i,j分别表示 s 1 , s 2 s_1,s_2 s1,s2 " A C " "AC" "AC"的个数,用 a a a表示 s 1 s_1 s1头部是否有‘A’,用 b b b表示 s 1 s_1 s1尾部是否有‘C’,用 c c c表示 s 2 s_2 s2头部是否有‘A’,用 d d d表示 s 2 s_2 s2尾部是否有‘C’;
f i f_i fi表示字符串 s i s_i si中"AC"的个数,那么, f 1 = i f_1=i f1=i, f 2 = j f_2=j f2=j,若 b + c = 2 b+c=2 b+c=2 f 3 = i + j + 1 f_3=i+j+1 f3=i+j+1,否则 f 3 = i + j f_3=i+j f3=i+j,继续推可以得到当 t > 3 t>3 t>3时的表达式 f t = f t − 1 + f t − 2 + f_t=f_{t-1}+f_{t-2}+ ft=ft1+ft2+((t%2==1 && d+c==2)||(t%2==0 && a+d==2))}
注意:不能 f 3 f_3 f3通过该递推式求出,要单独讨论,此时是 s 1 s_1 s1尾部与 s 2 s_2 s2头部相连,而 t > 3 t>3 t>3时是 s 1 s_1 s1头部与 s 2 s_2 s2尾部相连 ( ( (t为偶数 ) ) ) s 2 s_2 s2头部与 s 2 s_2 s2尾部相连 ( ( (t为奇数 ) ) );
代码如下:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int k,x,n,m,f[20005];
signed main()
{
    cin>>k>>x>>n>>m;
    for (int i=0;i<=n>>1;i++)
    for (int j=0;j<=m>>1;j++)//用i,j分别表示s1,s2中"AC"的个数
    {
        for (int a=0;a<=1;a++)
        for (int b=0;b<=1;b++)//用a表示s1头部是否有‘A’,用b表示s1尾部是否有‘C’,
        for (int c=0;c<=1;c++)
        for (int d=0;d<=1;d++)//用c表示s2头部是否有‘A’,用d表示s2尾部是否有‘C’;
        {
            if (2*i+a+b>n || 2*j+c+d>m) continue;//超过限制长度时返回
            f[1]=i;
            f[2]=j;
            f[3]=i+j+(b+c==2);
            for (int t=4;t<=k;t++)//通过递推得到sk中"AC"的数量
            {
                f[t]=f[t-1]+f[t-2]+(((t&1)==1 && d+c==2)||((t&1)==0 && a+d==2));
            }
            if (f[k]==x)//输出结果
            {
                if (a) cout<<"C";
                for (int t=1;t<=i;t++) cout<<"AC";
                for (int t=1;t<=n-2*i-a-b;t++) cout<<"X";//补充不足的位数
                if (b) cout<<"A";
                cout<<endl;
                if (c) cout<<"C";
                for (int t=1;t<=j;t++) cout<<"AC";
                for (int t=1;t<=m-2*j-c-d;t++) cout<<"X";
                if (d) cout<<"A";
                return 0;
            }
        }
    }
    cout<<"Happy new year!";//无法实现时输出Happy new year!
    return 0;
}

推荐一篇题解:
https://www.cnblogs.com/stepsys/p/10383485.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值