zoukankan      html  css  js  c++  java
  • ZOJ 3019 Puzzle

    解题思路:给出两个数列an,bn,其中an,bn中元素的顺序可以任意改变,求an,bn的LCS

                  因为数列中的元素可以按任意顺序排列,所以只需要求出an,bn中的元素有多少个是相同的即可。

    反思:一开始以为就是求LCS,一直WA,后来才发现可以按任意顺序排列元素,把相同的元素都排在一起,就是最长的子序列。

    Puzzle

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    For sequences of integers a and b, if you can make the two sequences the same by deleting some elements in a and b, we call the remaining sequence "the common sub sequence". And we call the longest one the LCS.

    Now you are given two sequences of integers a and b. You can arrange elements in a and b in any order. You are to calculate the max length of the LCS of each arrangement of a and b.

    Input

    Input will consist of multiple test cases. The first line of each case is two integers N(0 < N < 10000), M(0 < M < 10000) indicating the length of a and b. The second line is N 32-bit signed integers in a. The third line is 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 4
    1 2 3 2 1
    1 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
    ",num);		
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    累加和最大化
    SELECT子句顺序
    Linux命令入门教程
    求字符串前缀最长的连续重复子串(不重叠)
    王道机试指南题解(C/C++版)
    C++ Primer 第 5 版 习题参考答案
    第 16 章
    第 15 章
    第 14 章
    从jvm源码解读Java运行时的类加载
  • 原文地址:https://www.cnblogs.com/wuyuewoniu/p/4177414.html
Copyright © 2011-2022 走看看