SOJ-1016

/******************************************************************************************************
 ** Copyright (C) 2011.05.01 - 2013.07.01
 ** Author: famousDT <[email protected]>
 ** Edit date: 2011-04-25
******************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>//abs,atof(string to float),atoi,atol,atoll
#include <math.h>//atan,acos,asin,atan2(a,b)(a/b atan),ceil,floor,cos,exp(x)(e^x),fabs,log(for E),log10
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <string.h>//memcpy(to,from,count
#include <ctype.h>//character process:isalpha,isdigit,islower,tolower,isblank,iscntrl,isprint
#include <algorithm>
using namespace std;

//typedef long long int ll;

#define PI acos(-1)
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MALLOC(n, type) ((type *)malloc((n) * sizeof(type)))
#define FABS(a) ((a) >= 0 ? (a) : (-(a)))

int yes = 0;
int n;

void process(char s[], int ans)
{
    if (strcmp(s, "()") == 0)
        return ;
    if (s[0] == '(') {
        int tag = 0;
        int i;
        if (s[1] == '-') {
            tag = 1;
            i = 2;
        }
        else
            i = 1;
        int len = strlen(s);
        int tmp = 0;
        for (; i < len; ++i) {
            if (isdigit(s[i]))
                tmp = tmp * 10 + s[i] - '0';
            else
                break;
        }
        if (tag == 1)
            tmp = -tmp;
        //printf("%d\n", tmp);
        int test = 0;
        char left[10000];
        int index = 0;
        int flag = 0;
        for (; i < len; ++i) {
            if (s[i] == '(') {
                flag = 1;
                ++test;
            }
            if (s[i] == ')')
                --test;
            left[index++] = s[i];
            if (test == 0 && flag == 1)
                break;
        }
        left[index] = '\0';
        index = 0;
        char right[10000];
        for (++i; i < len - 1; ++i)
            right[index++] = s[i];
        right[index] = '\0';
        if (strcmp(left, "()") == 0 && strcmp(right, "()") == 0) {
            if (ans + tmp == n)
                yes = 1;
            return ;
        }
        process(left, ans + tmp);
        process(right, ans + tmp);
    }
}

int main()
{
    char s[10000];
    int index = 0;
    char ans[10000];
    while (cin>>n) {
        index = 0;
        yes = 0;
        int test = 0;
        while (gets(s) != NULL) {
            int len = strlen(s);
            int i;                    
            for (i = 0; i < len; ++i) {
                if (isspace(s[i]))
                    ;
                else {
                    ans[index++] = s[i];
                    if (s[i] == '(')
                        ++test;
                    if (s[i] == ')')
                        --test;
                }
            }
            if (test == 0)
                break;
        }
        ans[index] = '\0';
        int sum = 0;
        process(ans, sum);
        printf("%s\n", yes == 1 ? "yes" : "no");
    }
    return 0;
}

你可能感兴趣的:(SOJ-1016)