poj 1509 最小表示法
poj 2352 第一道树状数组和线段树

poj 2250 输出LCS

scturtle posted @ 2010年11月22日 05:45 in algorithm , 1882 阅读

求LCS(longest common subsequence),并输出.

这个输出自己写的还是很geilivable的啊!

#include <cstdio>
#include <cstring>
char s1[100][31];
char s2[100][31];
int d[101][101];
int stack[100];

int inline max(int a,int b){if(a>b) return a;else return b;}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in","r",stdin);
    freopen("out","w",stdout);
#endif
    int i,j,t,n1,n2,maxd;
    while(scanf("%s",s1[0])!=EOF)
    {
        n1=1;
        while(scanf("%s",s1[n1]),s1[n1][0]!='#')
            n1++;
        n2=0;
        while(scanf("%s",s2[n2]),s2[n2][0]!='#')
            n2++;
        maxd=0;
        for(i=0;i<=n1;i++)
            for(j=0;j<=n2;j++)
            {
                if(i==0||j==0) 
                    d[i][j]=0;
                else if(!strcmp(s1[i-1],s2[j-1]))
                    d[i][j]=d[i-1][j-1]+1;
                else
                    d[i][j]=max(d[i-1][j],d[i][j-1]);
                maxd=max(maxd,d[i][j]);
            }
        i=n1;j=n2;
        t=0;
        while(d[i][j])
        {
            if(!strcmp(s1[i-1],s2[j-1]))
            {
                stack[t++]=i-1;
                i--;j--;
            }else if(d[i-1][j]>d[i][j-1])
                --i;
            else --j;
        }
        while(t>1)
            printf("%s ",s1[stack[--t]]);
        printf("%s\n",s1[stack[0]]);
    }
}

 

JAC Board 10th Clas 说:
2023年9月23日 17:05

Jharkhand Students Start to Learn Several Important topics and Concepts of Jharkhand Board class Syllabus 2024 From in-Depth Articles and interactive Fascinating Lesson videos to Exhaustive List JAC Board 10th Class Syllabus 2024 of Resources for Jharkhand Board class new Syllabus 2024 Available in Hindi, English, Mathematics, Science, Social Science All Subjects.Those Students who are eagerly Searching for Jharkhand 10th class Syllabus 2024 can get All the Details From This Article, Jharkhand Academic Council is an state Government Agency has Provided All the Subject wise Syllabus on their official website Only, To help all the Exam Appearing Students.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter