Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

不知道怎么回事儿在HOJ就是A不掉,,在POJac了在HOJ是WA。。。

Posted by javawin at 2011-02-24 23:38:30 on Problem 2250
In Reply To:DP问题,只是输出有点问题。。。,终AC,代码如下 Posted by:cike123 at 2010-09-04 00:46:20
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
char map1[102][40],map2[102][40];
int dp[102][102];

struct node{
    char s[40];
    int i,j;
}DP[102][102];

void print(int i,int j){
    if(DP[i][j].s[0]=='#')return;

    if(DP[i][j].s[0]=='$'){
        print(DP[i][j].i,DP[i][j].j);
    }
    else{
          print(DP[i][j].i,DP[i][j].j);
          printf("%s ",DP[i][j].s);
    }
}


int main()
{
    int c1=1;
    int c2=1;
    while(scanf("%s",map1[c1])!=EOF){
      c1++;
      while(scanf("%s",map1[c1]),map1[c1][0]!='#')c1++;
      while(scanf("%s",map2[c2]),map2[c2][0]!='#')c2++;

      c1--;
      c2--;
      for(int i=0;i<102;i++){
          for(int j=0;j<102;j++)DP[i][j].s[0]='#';
      }
      for(int i=0;i<=c1;i++)dp[i][0]=0;
      for(int i=0;i<=c2;i++)dp[0][i]=0;


      for(int i=1;i<=c1;i++){
          for(int j=1;j<=c2;j++){
              if(strcmp(map1[i],map2[j])==0){
                  dp[i][j]=dp[i-1][j-1]+1;
                  DP[i][j].i=i-1;
                  DP[i][j].j=j-1;
                  strcpy(DP[i][j].s,map1[i]);
              }else{
                  if(dp[i-1][j]>dp[i][j-1]){
                      dp[i][j]=dp[i-1][j];
                      DP[i][j].s[0]='$';
                      DP[i][j].i=i-1;
                      DP[i][j].j=j;
                  }
                  else{
                      dp[i][j]=dp[i][j-1];
                      DP[i][j].s[0]='$';
                      DP[i][j].i=i;
                      DP[i][j].j=j-1;
                  }
              }
          }
      }

      /*for(int i=1;i<=c1;i++){
          for(int j=1;j<=c2;j++)printf("%s ",DP[i][j].s);
          printf("\n");
      }*/
       print(c1,c2);
       printf("\n\n");
     c1=1;
     c2=1;
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator