Problem-solving ideas: Give two series An,bn, in which the order of elements in an,bn can be arbitrarily changed, to seek an,bn LCS
Since the elements in the sequence can be arranged in any order, it is only necessary to ask how many of the elements in the an,bn are the same.
Reflection: At first thought is to seek LCS, has been WA, later found that you can arrange the elements in any order, the same elements are all together, is the longest sub-sequence.
Puzzle Time limit: 2 Seconds Memory Limit: 65536 KB
For sequences of integers a and B, if you can make the and the other sequences the same by deleting some elements in A and B, we cal L The remaining sequence "the common sub sequence". And we call the longest one the LCS.
Now is given, sequences of integers a and b. You can arrange elements in a and b in any order. You is to calculate the max length of the LCS for each arrangement of a and b.
Input
Input would consist of multiple test cases. The first line of each case is a integers N (0 < n < 10000), M (0 < M < 10000) indicating the length of a and b. The second line was N 32-bit signed integers in a. The third line was M 32-bit signed integers in b.
Output
Each case one line. The max length of the LCS of each arrangement of a and b.
Sample Input
5 41 2 3 2 11 4 2 1
Sample Output
3
#include <stdio.h> int a[10010],b[10010];void sort (int a[],int n) {int i,j,t;for (i=0;i<n-1;i++) {for (j=i+ 1;j<n;j++) {if (A[i]>a[j]) {t=a[i];a[i]=a[j];a[j]=t;}}}} int main () {int n,m,i,j,num,k;while (scanf ("%d%d", &n,&m)!=eof) {for (i=0;i<n;i++) scanf ("%d", &a[i]); for (i=0;i<m;i++) scanf ("%d", &b[i]); sort (a,n); sort (b,m); Num=0;k=0;for (i=0;i<n;i++) {for (j=k;j<m;j++) {if (A[i]==b[j]) {num++;k=j+1;break; }}} printf ("%d\n", num);} return 0;}
ZOJ 3019 Puzzle