(UVa) 10192 – Vacation

0
/*
user: php
time: 0.008 sec
link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1133
*/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>

#include<algorithm>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>
#include<sstream>



#define FOR(i, s, e) for(int i=s; i<e; i++)
#define loop(i, n) for(int i=0; i<n; i++)
#define getint(n) scanf("%d", &n)
#define pb(a) push_back(a)
#define ll long long
#define SZ(a) int(a.size())
#define read() freopen("input.txt", "r", stdin)
#define write() freopen("output.txt", "w", stdout)
#define mem(a, v) memset(a, v, sizeof(a))
#define all(v) v.begin(), v.end()
#define pi acos(-1.0)
#define INF 1<<29
#define mod(a) (a>0?a:-a)
#define pf printf
#define sf scanf

using namespace std;

#define MAXX 102

char frst[MAXX], scnd[MAXX];
int dp[MAXX][MAXX];
int frlen, sclen;

int rec(int i, int j)
{
    if(i>=frlen || j>=sclen) return 0;
    int &ret = dp[i][j];
    if(ret != -1) return ret;

    if( frst[i] == scnd[j] )
    {
        return ret = 1 + rec(i+1, j+1);
    }
    else
    {
        return ret = max(rec(i, j+1), rec(i+1, j));
    }
}



int main()
{
    int kaseno = 0;
    while(true)
    {
        gets(frst);
        frlen = strlen(frst);
        if(frst[0] == '#' && frlen == 1)
        {
            break;
        }
        gets(scnd);
        sclen = strlen(scnd);

        mem(dp, -1);
        pf("Case #%d: you can visit at most %d cities.\n", ++kaseno, rec(0, 0));




    }


    return 0;
}






(UVa) 10282 – Babelfish

0
/*
user: php
time: 0.444 sec
link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1223
*/

#include<iostream>
#include<map>
#include<string>
#include<cstdio>
#include<cstring>

using namespace std;

map<string, string> dic;



int main()
{
    string english, foreign;
    char inp[100];
    char *ptr;
    while(gets(inp))
    {
        if(strlen(inp) == 0) break;

        ptr = strtok(inp, " ");
        english = ptr;
        ptr = strtok(NULL,"");
        foreign = ptr;
        dic[foreign] = english;
    }

    while(gets(inp))
    {
        foreign = inp;
        if(dic[foreign].length() == 0)
        {
            printf("eh\n");
        }
        else
        {
            printf("%s\n", dic[foreign].c_str());
        }
    }

    return 0;

}


(UVa) 10664 – Luggage

0
/*
problem link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=24&problem=1605&mosmsg=Submission+received+with+ID+10809486

Username: php
Time: 0.008s
*/


#include<iostream>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
using namespace std;
#define MAX 200
#define pb push_back
#define SZ size()


void free(int ara[])
{
    for(int i=0; i<MAX; i++)
    {
        ara[i] = 0;
    }
}


int main()
{
    int load[MAX];
    int cases;
    char *ptr, str[10000];
    int i;
    int totalSum;
    queue<int> Q;


    cin>>cases;



    gets(str);


    while(cases--)
    {
        free(load);
        load[0] = 1;
        while(Q.SZ)
        {
            Q.pop();
        }
        totalSum = 0;




        gets(str);

        for(ptr = strtok(str, " "); ptr; ptr = strtok(NULL, " "))
        {
            i = atoi(ptr);
            Q.push(i);
            totalSum += i;
        }
        if(totalSum % 2 || totalSum == 0)
        {
            cout<<"NO"<<endl;
            continue;
        }
        totalSum = totalSum / 2;

        while(Q.SZ)
        {
            i = Q.front();
            for(int j = totalSum-1; j>-1; j--)
            {
                if(i+j <= totalSum && load[j] != 0)
                {
                    load[i+j] = load[i] + load[j];
                }
            }
            Q.pop();
        }

        if(load[totalSum] != 0)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;




    }
}