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 |
郁闷,输出时没把最后一个的空格去掉,WA了好几次。题是好题,经典的DP郁闷,输出时没把最后一个的空格去掉,WA了好几次。题是好题,经典的DP,跟1458题型一样。 #include <iostream> #include <vector> #include <string> #include <cmath> #include <algorithm> #include <functional> #include <list> #include <map> #include <set> using namespace std; char s[32]; string str1[110],str2[110],ans[110][110],temp; int len1,len2,DP[110][110]; class POJ { public: int POJMethod() { int i,j,k; memset(DP,0,sizeof(DP)); DP[0][0] = 0; ans[0][0] = ""; for( i = 1;i <= len1;++i) {DP[i][0] = 0;ans[i][0] = "";} for( i = 1;i <= len2;++i) {DP[0][i] = 0;ans[0][i] = "";} for( i = 1;i <= len1;++i) for( j = 1;j <= len2;++j) { if(str1[i] == str2[j]) { DP[i][j] = DP[i-1][j-1] + 1; ans[i][j] = ans[i-1][j-1] + str1[i] + " "; } else { DP[i][j] = DP[i-1][j]>DP[i][j-1] ? DP[i-1][j] : DP[i][j-1]; ans[i][j] = DP[i-1][j]>DP[i][j-1] ? ans[i-1][j] : ans[i][j-1]; } } //cout<<ans[len1][len2]<<endl; 直接这样输出会WA,用以下3句代替吧-_- int len = ans[len1][len2].size(); temp=ans[len1][len2]; cout<<temp.erase(len-1,1)<<endl; return 0; } }; int main() { while(scanf("%s",s)!=EOF) { len1 = 0; while(s[0]!='#') { ++len1; str1[len1] = s; cin>>s; } len2 = 0; cin>>s; while(s[0]!='#') { ++len2; str2[len2] = s; cin>>s; } POJ test; test.POJMethod(); } system("pause"); return 0; }; Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator