uva 497 Strategic Defense Initiative

求最长公共子序列

由于这道题简单,方法多样,博主选了dp做

博主为朱老师的弟子

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
//#define T
int num1[100000];
int num2[100000];
int num3[10000][10000];
int m;
char ch[250];
void initial()
{
    memset(num1,0,sizeof(num1));
    memset(num2,0,sizeof(num2));
    memset(num3,0,sizeof(num3));
    memset(ch,'\0',sizeof(ch));
    m = 0;
}

int return_index()
{
    int d = 0;
    for(int i =1;i < m;i++)
    {
        if(num1[i] < num1[m])
        {
            if(num2[i] > num2[m])
            {
                num2[m] = num2[i];
                d = i;
            }
        }
    }
    return d;

}

void push_bback(int index)
{
    int i = 1;
    for( i = 1;i < m;i++)
    {
        num3[m][i] = num3[index][i];
    }
    num3[m][i] = 1;
    num2[m] = num2[index] + 1;
}
void print_num()
{
    int i ;
    int MAX = 0;
    int index;
    for(i = 1;i<=m;i++)
    {
        if(num2[i] > MAX)
        {
            index = i;
            MAX = num2[i];
        }
    }
    printf("Max hits: %d\n",MAX);
    for(i = 1;i<=m;i++)
    {
        if(num3[index][i])
        {
            printf("%d\n",num1[i]);
        }
    }

}
int main()
{
    #ifdef T
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif // T
    int n,t=0;
   // char ch[250];
    scanf("%d",&n);
    getchar();
    getchar();
    for(t = 0; t < n;t++)
    {
        initial();
        int in;
        if(t!=0)
            printf("\n");
        while(gets(ch)&&ch[0]!='\0')
        {
            m++;
            //ch = getchar();
            //printf("%c\n",ch);
           sscanf(ch,"%d",&num1[m]);
            //printf("%d ------ num1     ---- %d ---- m\n",num1[m],m);
            in = return_index();
           // printf("in--------%d\n",in);
            push_bback(in);



        }
        print_num();

    }
    return 0;
}


你可能感兴趣的:(uva 497 Strategic Defense Initiative)