/* FILE: maxCommonSubsequence3Only2.cpp last change: 23-Jan-2013 author: Romeo Rizzi * a solver for problem maxCommonSubsequence3 assuming the third input string is the concatenation of the first two (whence can be simply disregarded) */ #define NDEBUG // NDEBUG definita nella versione che consegno #include #ifndef NDEBUG # include // uso di cin e cout non consentito in versione finale #endif #include using namespace std; const int MAX_N = 100; // massima cardinalita' delle 3 stringhe in input char s1[MAX_N], s2[MAX_N], s3[MAX_N]; // le 3 stringhe in input int n1, n2, n3; // lunghezza delle 3 stringhe in input int opt[MAX_N +1][MAX_N +1]; /* opt[i][j] = the maximum length of a common subsequence of the two prefixes s1[0..i] and s2[0..j]. */ int main() { ifstream fin("input.txt"); assert( fin ); fin >> n1 >> n2 >> n3; for(int i = 0; i < n1; i++) fin >> s1[i]; for(int i = 0; i < n2; i++) fin >> s2[i]; fin.close(); for(int i = n1; i >= 0; i--) for(int j = n2; j >= 0; j--) if( (i == n1) || (j == n2) ) opt[i][j] = 0; else if( s1[i] != s2[j] ) opt[i][j] = max( opt[i+1][j], opt[i][j+1] ); else // s1[i] == s2[j] opt[i][j] = 1 + opt[i+1][j+1]; ofstream fout("output.txt"); fout << opt[0][0] << endl; fout.close(); return 0; }