Codeforces Round #300 A, B(背包dp), ..解题报告

A - Cutting Banner
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

A large banner with word CODEFORCES was ordered for the 1000-th onsite round of Codeforcesω that takes place on the Miami beach. Unfortunately, the company that made the banner mixed up two orders and delivered somebody else's banner that contains someone else's word. The word on the banner consists only of upper-case English letters.

There is very little time to correct the mistake. All that we can manage to do is to cut out some substring from the banner, i.e. several consecutive letters. After that all the resulting parts of the banner will be glued into a single piece (if the beginning or the end of the original banner was cut out, only one part remains); it is not allowed change the relative order of parts of the banner (i.e. after a substring is cut, several first and last letters are left, it is allowed only to glue the last letters to the right of the first letters). Thus, for example, for example, you can cut a substring out from string 'TEMPLATE' and get string 'TEMPLE' (if you cut out string AT), 'PLATE' (if you cut out TEM), 'T' (if you cut out EMPLATE), etc.

Help the organizers of the round determine whether it is possible to cut out of the banner some substring in such a way that the remaining parts formed word CODEFORCES.

Input

The single line of the input contains the word written on the banner. The word only consists of upper-case English letters. The word is non-empty and its length doesn't exceed 100 characters. It is guaranteed that the word isn't word CODEFORCES.

Output

Print 'YES', if there exists a way to cut out the substring, and 'NO' otherwise (without the quotes).

Sample Input

Input
CODEWAITFORITFORCES
Output
YES
Input
BOTTOMCODER
Output
NO
Input
DECODEFORCES
Output
YES
Input
DOGEFORCES
Output
NO

a题注意只能切下来一部分,那么就只剩下三种情况,

CODEFORCES在最前面,

在最后面,

在两边(切掉中间),

这就很好判断了。

代码之后补。


B - Quasi Binary
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011 — are quasibinary and numbers 2, 12, 900 are not.

You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.

Input

The first line contains a single integer n (1 ≤ n ≤ 106).

Output

In the first line print a single integer k — the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.

In the second line print k numbers — the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.

Sample Input

Input
9
Output
9
1 1 1 1 1 1 1 1 1
Input
32
Output
3
10 11 11

两种姿势,

第一种,从最高位开始,每位不为0的数字减一,则一个元素为,对应位数的数字为1,其余数字为0,的数。

最高位减至0后将最高位设成下一个不为0的最高位。

#include<iostream>
#include<vector>
#include<fstream>
using namespace std;
ifstream fin("cin.in");
ofstream fout("right.out");
int n;
int a[8];
vector<int> ans;
void solve()
{
    ans.clear();
    int i=0, j=0, t=0;
    while(n){
        a[i++]=n%10;
        n/=10;
    }

    while(i){
        t=0;
        if(a[i-1]==0) i--;
        for(j=i-1;j>=0;j--){
            t*=10;
            if(a[j]){
                a[j]--;
                t++;
            }
        }

        if(t) ans.push_back(t);
    }
    fout<<ans.size()<<endl;
    for(vector<int>::iterator it=ans.begin();it!=ans.end();it++){
        if(it!=ans.begin()) fout<<' ';
        fout<<*it;
    }
    fout<<'\n';
}
int main()
{
    while(fin>>n){

        solve();
    }
}

第二种,背包dp,枚举物品,即从1到2^6的二进制数看作十进制的数(小于n),枚举背包容量,

由于要输出元素,所以将dp数组定义为结构体,期中用vis来记录路径。


#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<algorithm>
#include<fstream>

using namespace std;

const int maxn=1e6+7;
int n, m;
struct node
{
    int a;
    int vis;
}dp[maxn] ;


int change(int x)
{
    int y=1;
    int t=0;
    while(x){
        t+=x%2*y;
        x/=2;
        y*=10;
    }
    return t;
}

void init()
{
    for(int i=1;i<=n;i++){
        dp[i].a=i;
        dp[i].vis=i-1;
    }
}
void solve()
{
    init();
    for(int i=2;i< (2<<6)+2;i++){
        m=change(i);
        if(m>n) break;
        for(int j=1;j<=n;j++){

            /*for(int k=1;m*k<=j;k++){

                if(m*k<=j) {
                    if(dp[j].a>=dp[j-m*k].a+k){
                        dp[j].a=dp[j-m*k].a+k;
                        dp[j].vis=j-m*k;
                    }

                }
            }*/

            if(m<=j){
                if(dp[j].a>=dp[j-m].a+1){
                    dp[j].a=dp[j-m].a+1;
                    dp[j].vis=j-m;
                }
            }
        }
    }
    cout<<dp[n].a<<endl;
    int cur=n;
    while(cur){
        cout<<cur-dp[cur].vis;
        cur=dp[cur].vis;
        if(cur) cout<<' ';
    }
    cout<<'\n';

}
int main()
{

    while(cin>>n){

        solve();

    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值