【动态规划】UVALive

f(i,j)表示从A序列前面取i个,从B序列前面取j个时,能否拼成C序列。转移自行脑补。

A train yard is a complex series of railroad tracks for storing, sorting, or loading/unloading railroad cars. In this problem, the railroad tracks are much simpler, and we are only interested in combining two trains into one.

merging tracks

Figure 1: Merging railroad tracks.

The two trains each contain some railroad cars. Each railroad car contains a single type of products identified by a positive integer up to 1,000,000. The two trains come in from the right on separate tracks, as in the diagram above. To combine the two trains, we may choose to take the railroad car at the front of either train and attach it to the back of the train being formed on the left. Of course, if we have already moved all the railroad cars from one train, then all remaining cars from the other train will be moved to the left one at a time. All railroad cars must be moved to the left eventually. Depending on which train on the right is selected at each step, we will obtain different arrangements for the departing train on the left. For example, we may obtain the order 1,1,1,2,2,2 by always choosing the top train until all of its cars have been moved. We may also obtain the order 2,1,2,1,2,1 by alternately choosing railroad cars from the two trains.

To facilitate further processing at the other train yards later on in the trip (and also at the destination), the supervisor at the train yard has been given an ordering of the products desired for the departing train. In this problem, you must decide whether it is possible to obtain the desired ordering, given the orders of the products for the two trains arriving at the train yard.

Input

The input consists of a number of cases. The first line contains two positive integers N1 N2 which are the number of railroad cars in each train. There are at least 1 and at most 1000 railroad cars in each train. The second line contains N1 positive integers (up to 1,000,000) identifying the products on the first train from front of the train to the back of the train. The third line contains N2 positive integers identifying the products on the second train (same format as above). Finally, the fourth line contains N1+N2positive integers giving the desired order for the departing train (same format as above).

The end of input is indicated by N1 = N2 = 0.

Output

For each case, print on a line possible if it is possible to produce the desired order, or not possible if not.

Sample input

3 3
1 2 1
2 1 1
1 2 1 1 2 1
3 3
1 2 1
2 1 2
1 1 1 2 2 2
0 0

Sample Output

possible
not possible
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,a[1010],b[1010],c[2010];
bool f[1010][1010];
int main()
{
	while(1)
	  {
	  	scanf("%d%d",&n,&m);
	  	if((!n)&&(!m))
	  	  break;
	  	for(int i=1;i<=n;++i)
	  	  scanf("%d",&a[i]);
	  	for(int i=1;i<=m;++i)
	  	  scanf("%d",&b[i]);
	  	for(int i=1;i<=n+m;++i)
	  	  scanf("%d",&c[i]);
	  	memset(f,0,sizeof(f));
	  	f[0][0]=1;
	  	for(int i=0;i<=n;++i)
	  	  for(int j=0;j<=m;++j)
	  	    {
	  	      if(i>0&&j>0)
	  	        {
	  	          if((f[i-1][j] && a[i]==c[i+j]) || (f[i][j-1] && b[j]==c[i+j]))
	  	            f[i][j]=1;
	  	        }
	  	      else if(i==0&&j>0)
	  	        {
	  	          if(f[i][j-1] && b[j]==c[i+j])
	  	            f[i][j]=1;
	  	        }
	  	      else if(i>0&&j==0)
	  	        {
	  	          if(f[i-1][j] && a[i]==c[i+j])
	  	            f[i][j]=1;
	  	        }
	  	    }
	  	puts(f[n][m] ? "possible" : "not possible");
	  }
	return 0;
}
原文地址:https://www.cnblogs.com/autsky-jadek/p/6291564.html