alwaysBeAStarter

博客园 首页 新随笔 联系 订阅 管理

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=4&page=show_problem&problem=167

这道题实际上就是在一个序列中找出最长的非降sub序列。第一个序列就是原序列,第二个是排序过的非降序列,然后找出Longest common substring,就是答案。

代码如下:

#include <iostream>
#include <math.h>
#include <stdio.h>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cstring>
#include <queue>
#include <vector>
#include <functional>
#include <cmath>
#define SCF(a) scanf("%d", &a)
#define IN(a) cin>>a
#define FOR(i, a, b) for(int i=a;i<b;i++)
typedef long long Int;
using namespace std;

bool cmp(int a, int b)
{
    if (a > b)
        return true;
    return false;
}

int main()
{
    vector<int> missiles, ordered;
    int num = 0;
    int n;
    int test = 0;
    while (SCF(n))
    {
        if (n == -1 && num == 0)
            break;
        else if (n == -1)
        {
            sort(ordered.begin(), ordered.begin() + num, cmp);
            int** match;
            match = new int*[num + 1];
            FOR(i, 0, num + 1)
                match[i] = new int[num + 1];
            FOR(i, 0, num + 1)
            {
                match[i][0] = 0;
                match[0][i] = 0;
            }

            FOR(i, 1, num + 1)
            {
                FOR(j, 1, num + 1)
                {
                    if (ordered[i - 1] == missiles[j - 1])
                        match[i][j] = match[i - 1][j - 1] + 1;
                    else
                        match[i][j] = max(match[i][j - 1], match[i - 1][j]);
                }
            }
            if (test > 0)
                printf("\n");
            printf("Test #%d:\n", ++test);
            printf("  maximum possible interceptions: %d\n", match[num][num]);
            while (!missiles.empty())
            {
                missiles.pop_back();
            }
            while (!ordered.empty())
            {
                ordered.pop_back();
            }
            num = 0;
        }
        else
        {
            missiles.push_back(n);
            ordered.push_back(n);
            num++;
        }
    }
    return 0;
}

 

posted on 2017-06-12 20:06  alwaysBeAStarter  阅读(108)  评论(0编辑  收藏  举报