首页 > ACM题库 > HDU-杭电 > hdu 2263 Balance[解题报告]C++
2014
01-04

hdu 2263 Balance[解题报告]C++

Balance

问题描述 :

You are given a set of weight values and asked to measure something with a certain weight. Weight x is measurable with a set y of weight values if and only if you can do the following: Place one weight with value x on either the left or right side of the scale. Then place some combination of weights on the scale with values from the set y so that the left and right sides have the same total value. You may use each value in y zero or more times to achieve this. In order to reach the target, you choose a kind of weight randomly and try to measure with it. If you find it impossible, you choose an additional kind and try again until you can measure with the set of weights you have chosen. Now, please tell me the expect kinds of weight you have to choose.

输入:

For each case, the first line is two integers n and m (1<=n<=15), which n represents the number of weights you have and m is the weight you have to measure. Followed by n positive integers ai (1<=i<=n), indicating the mass of each kind of weight. 0 < m,ai < 2^31.

输出:

For each case, the first line is two integers n and m (1<=n<=15), which n represents the number of weights you have and m is the weight you have to measure. Followed by n positive integers ai (1<=i<=n), indicating the mass of each kind of weight. 0 < m,ai < 2^31.

样例输入:

1 2
3
3 3
1 2 3
3 5
2 3 4

样例输出:

-1
1.333
2.333

竟然会wa,检查了n遍才发现YES只有y是大写的

#include<stdio.h>
#include<string.h>
#include<ctype.h>
char s[1000];
int main()
{
    int n;
    char *p,*_p;
    scanf("%d",&n);
    getchar();
    while(n-->0)
    {
        gets(s);
        p=s;
        while(*p!='\0')
        {
            if(*p=='('&&*(p+1)==')'||(*p=='['&&*(p+1)==']'))
            {
                *p='\0';
                _p=p+2;
                p=s;
                strcat(p,_p);
            }
            else
                p++;
        }
        if(s[0]=='\0')
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}

解题转自:http://www.cnblogs.com/xiaocai905767378/archive/2011/05/29/2062007.html


  1. a是根先忽略掉,递归子树。剩下前缀bejkcfghid和后缀jkebfghicd,分拆的原则的是每个子树前缀和后缀的节点个数是一样的,根节点出现在前缀的第一个,后缀的最后一个。根节点b出现后缀的第四个位置,则第一部分为四个节点,前缀bejk,后缀jkeb,剩下的c出现在后缀的倒数第2个,就划分为cfghi和 fghic,第3部分就为c、c

  2. L(X [0 .. M-1],Y [0 .. N-1])= 1 + L(X [0 .. M-2],Y [0 .. N-1])这个地方也也有笔误
    应改为L(X [0 .. M-1],Y [0 .. N-1])= 1 + L(X [0 .. M-2],Y [0 .. N-2])

  3. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。