HDOJ_1160_FatMouse's Speed

 单词:

disprove  vt.  反驳; 证明…是虚假的;

mice  n.  老鼠( mouse的名词复数 ); 鼠标; 羞怯[胆小]的人; mouse的复数形式;

subset  n.  子集;

grams  n.  克( gram的名词复数 );

centimeters  n.  厘米( centimeter的名词复数 );

Problem Description

A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x2, ..., xm> another sequence Z = <z1, z2, ..., zk> is a subsequence of X if there exists a strictly increasing sequence <i1, i2, ..., ik> of indices of X such that for all j = 1,2,...,k, xij = zj. For example, Z = <a, b, f, c> is a subsequence of X = <a, b, c, f, b, c> with index sequence <1, 2, 4, 6>. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y. The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.

Sample Input

abcfbc abfcab
programming contest 
abcd mnp

Sample Output

4
2
0


思路:首先对重量进行排序,然后再找出最长路径。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define Max 1005
using namespace std;

struct Mice
{
    long long weight;
    long long speed;
    int sequence;
}mice[Max];

struct DP
{
    int mommax; 
    int max;
    int pre;
}dp[Max];

int a[Max];

bool compare(Mice a,Mice b)
{
    if(a.weight==b.weight)
        return a.speed>b.speed;
    else
        return a.weight<b.weight;
}


int main(void)
{
    freopen("in.txt","r",stdin);
    
    memset(mice,0,sizeof(mice));
    memset(dp,0,sizeof(dp));
    
    int i=1,n=0;
    while(scanf("%lld %lld",&mice[i].weight,&mice[i].speed)!=EOF)
    {
        mice[i].sequence=i;
        i++;
        n++;
    }
    
    sort(mice+1,mice+1+n,compare);
    
    int mmax=1,count=1;
    dp[1].max=1;
    for(i=2;i<=n;i++)
    {
        dp[i].max=1,dp[i].mommax=1;
        for(int j=i-1;j>=1;j--)
        {
            if(mice[i].weight>mice[j].weight&&mice[i].speed<mice[j].speed)
                if(dp[i].max<dp[i].mommax+dp[j].max)
                {
                    dp[i].max=dp[i].mommax+dp[j].max;
                    dp[i].pre=j;
                }
        }
        
        if(mmax<dp[i].max)
        {
            mmax=dp[i].max;
            count=i;
        }
    }
    
    i=1;
    printf("%d\n",mmax);
    while(dp[count].pre!=0)
    {
        a[i++]=count;
        count=dp[count].pre;
    }
    a[i]=count;
    
    for(;i>=1;i--)
        printf("%lld\n",mice[a[i]].sequence);
    
    
    
    
    
    fclose(stdin);
    return 0;
}

 

posted @ 2018-12-19 05:51  pha创噬  阅读(111)  评论(0编辑  收藏  举报