Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
AC,贴个递归代码#include <iostream> #include <string> #include <vector> #include <memory.h> #include <algorithm> using namespace std; int dp[1000][1000]; int b[1000][1000]; vector <string> seq; vector <string> s1, s2; int lcs( int i, int j ) { if ( i == 0 && j >= 0 ) return 0; else if ( j == 0 && i >= 0 ) return 0; else { if ( s1[i] == s2[j] ) { b[i][j] = 0; if ( dp[i-1][j-1] == -1 ) { dp[i-1][j-1] = lcs( i-1, j-1 ); } return dp[i-1][j-1]+1; } else { if ( dp[i][j-1] == -1 ) dp[i][j-1] = lcs( i, j-1 ); if ( dp[i-1][j] == -1 ) dp[i-1][j] = lcs( i-1, j ); if ( dp[i][j-1] > dp[i-1][j] ) { dp[i][j] = dp[i][j-1]; b[i][j] = 2; } else { dp[i][j] = dp[i-1][j]; b[i][j] = 1; } return dp[i][j]; } } } void print_lcs ( int i, int j ) { if ( i == 0 || j == 0 ) return; if ( b[i][j] == 0 ) { print_lcs ( i-1, j-1 ); cout<<s1[i]<<" "; } else if ( b[i][j] == 1 ) print_lcs ( i-1, j ); else print_lcs ( i, j-1 ); } int main() { int i, j, step = 1, clr = 0; string s; s1.push_back("1"); s2.push_back("2"); while ( cin>>s ) { if ( step == 1 ) { if ( s != "#" ) { s1.push_back( s ); } else { step = 2; } } else { if ( s != "#" ) { s2.push_back( s ); } else { step = 1; seq.clear(); memset ( dp, -1, sizeof(int)*1000000 ); memset ( b, -1, sizeof(int)*1000000 ); lcs( s1.size()-1, s2.size()-1 ); print_lcs( s1.size()-1, s2.size()-1 ); cout<<endl; s1.clear(); s2.clear(); s1.push_back("1"); s2.push_back("2"); } } } return 1; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator