[CSP-S Simulation Test]: reverse (analog)

Portal Title (title 56 inside)


Input Format

The first row contains an integer: $ T $, represents the number of data sets.
Then $ T $ rows each comprising two strings: $ a \ b $.


Output Format

For each test, if $ c $, lexicographically $ c $ maximum output under the presence of the longest, or -1 output $ $.


Sample

Sample input:

3
AB BA
ABA Bab
AB started

Sample output:

-1
AB
AB


Data range and tips

Sample explained:

For the first set of data, such as $ c $ does not exist.
For the second group of data, $ AB $ in a first operation to $ ABA $, $ AB $ by a second operation to $ $ BAB-by.
For the third set of data, $ AB $ no operator can get $ AB $, $ AB $ second operation is performed twice to obtain $ ABAA $, $ and $ AB is the length of the longest minimum satisfies lexicographically $ c $ conditions.

data range:

For $ 10 \% $ data, $ 1 \ leqslant | a | <| b | \ leqslant 6 $;
for $ 30 \% $ data, $ 1 \ leqslant | a | <| b | \ leqslant 12 $;
for $ 100 \% data $, $ 1 \ leqslant | a | <| b | \ leqslant 2,000,1 \ leqslant T \ leqslant 20 $, guaranteed $ a, b $ are from $ a, B $ characters.


answer

This question of the difficulty lies in how let lexicographical problem.

In fact, we do not have to consider the issue of lexicographical, which can be seen from two points.

  $ \ Alpha. $ Title and sample interpretation says is the smallest lexicographically, but the output format was written lexicographically largest, coach and did not change the title, so do not consider this question lexicographical problem.

  $ \ Beta. $ Simulation about the process, can only be transferred from a string of one way over, so we can look back, and for a length we can only look back a string, so we do not consider the lexicographical problem.

To solve this problem is a big simulation, in fact, you can use $ $ hash achieve linear time complexity, but obviously out of question mark written by Cheng also steal a lazy.

Time complexity: $ \ Theta (n ^ 2) $.

Expectations score: $ 100 $ points.

Actual score: $ 100 $ points.


Code time

#include<bits/stdc++.h>
using namespace std;
char ch1[2001],ch2[2001];
int a[2001],b[2001];
bool judge(){for(int i=1;i<=a[0];i++)if(a[i]!=b[i])return 0;return 1;}
int main()
{
	int T;scanf("%d",&T);
	while(T--)
	{
		scanf("%s%s",ch1+1,ch2+1);
		a[0]=strlen(ch1+1);
		b[0]=strlen(ch2+1);
		for(int i=1;i<=a[0];i++)a[i]=ch1[i]-'A';
		for(int i=1;i<=b[0];i++)b[i]=ch2[i]-'A';
		if(a[0]>b[0])swap(a,b);
		while(b[0]>a[0])
		{
			if(b[b[0]])
			{
				b[0]--;
				reverse(b+1,b+b[0]+1);
			}
			else b[0]--;
		}
		while(1)
		{
			if(judge())
			{
				for(int i=1;i<=b[0];i++)
					if(b[i])printf("B");
					else printf("A");
				puts("");
				break;
			}
			if(a[a[0]])
			{
				a[0]--;
				reverse(a+1,a+a[0]+1);
			}
			else a[0]--;
			if(b[b[0]])
			{
				b[0]--;
				reverse(b+1,b+b[0]+1);
			}
			else b[0]--;
			if(!a[0]&&!b[0]){puts("-1");break;}
		}	
	}
	return 0;
} 

rp++

Guess you like

Origin www.cnblogs.com/wzc521/p/11586328.html