Haven't done OJ in 5 years,
'd been through water, remove Wushan not cloud "
Prepare to brush 1-2 questions per week!
Title: Give n Strings, and each string contains a unique letter, that is, there is no "ABCA" (a repetition), and "AFDSG" is correct.
Find the common letters of n strings. Finally, the output is in dictionary order.
Analysis: First of all the strings of the dictionary ordering, and then ask all the LCS, the practice is 22-phase request. n Strings, a total of N-1 times LCS, the final result is obtained.
Code
//http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3603#include <iostream>#include<stack>#include<cstdio>#include<algorithm>#include<string>#include<fstream>using namespacestd;intmatrix[ -][ -];intf[ -][ -];stringresult ="";voidSubsequence (intIintJstringstr) { if(i = =0|| j = =0)return; if(F[i][j] = =1) {result= Str[i-1] +result; Subsequence (i-1, J-1, str); } Else { if(F[i][j] = =2) {subsequence (i-1, J, str); } Else{subsequence (i, J-1, str); } }}stringLCS (stringSTR1,stringstr2) { //initializing boundaries, filtering out 0 of cases for(inti =0; I <= str1.size (); i++) matrix[i][0] =0; for(intj =0; J <= Str2.size (); J + +) matrix[0][J] =0; for(inti =0; I < A; i++) { for(intj =0; J < A; J + +) {F[i][j]=0; } } //Fill Matrix for(inti =1; I <= str1.size (); i++) { for(intj =1; J <= Str2.size (); J + +) { if(Str1[i-1] = = Str2[j-1]) {Matrix[i][j]= Matrix[i-1][j-1] +1; F[I][J]=1; } Else { if(Matrix[i-1][J] >= matrix[i][j-1]) {Matrix[i][j]= Matrix[i-1][j]; F[I][J]=2; } Else{Matrix[i][j]= Matrix[i][j-1]; F[I][J]=3; } }}} result=""; Subsequence (Str1.size (), Str2.size (), str1); returnresult;}intMain () {//fstream cin ("3603.txt"); intT, N; CIN>>T; while(t--) {cin>>N; Stack<string> st = stack<string>(); while(!St.empty ()) {St.pop (); } stringtmp; for(inti =0; I < n; i++) {cin>>tmp; Sort (Tmp.begin (), Tmp.end ()); if(!St.empty ()) { stringInner =St.top (); St.pop (); St.push (LCS (inner, TMP)); } Else{St.push (TMP); }} cout<< st.top () <<Endl; } return 0;}
ZOJ 3603 DP LCS