UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#201019#458. sequenceyizhiming100213ms79528kbC++111.8kb2024-01-18 09:04:192024-01-18 12:04:32

answer

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
const int N = 4005;
int n,m;
char A[N],B[N];
int f[N][N];//匹配到了i,j时还需加多少个数 
int nxta[N][2],nxtb[N][2];
int pos[2];
bool id[N][N];
void print(int x,int y){
	if(x==n+1&&y==m+1){
		return;
	}
	cout<<id[x][y];
	print(nxta[x][id[x][y]],nxtb[y][id[x][y]]);
}
int main(){
	scanf("%s",A+1);
	scanf("%s",B+1);
	n = strlen(A+1);m = strlen(B+1);
	nxta[n+1][0] = nxta[n+1][1] = n+1;
	nxtb[m+1][0] = nxtb[m+1][1] = m+1;
//	cout<<"jrer\n";
	pos[0] = pos[1] = n+1;
	A[0] = B[0] = '0';
	for(int i=n;i>=0;i--){
		nxta[i][0] = pos[0];nxta[i][1] = pos[1];
		pos[A[i]-'0'] = i;
//		cout<<"I:"<<i<<"\n";
	}
//	cout<<"jre\n";
	pos[0] = pos[1] = m+1;
	for(int i=m;i>=0;i--){
		nxtb[i][0] = pos[0];nxtb[i][1] = pos[1];
		pos[B[i]-'0'] = i;
	}
//	memset(f,0x3f,sizeof(f));
	f[n+1][m+1] = 0;
	for(int i=n+1;i>=0;i--){
		for(int j=m+1;j>=0;j--){
			if(i==n+1&&j==m+1){
				continue;
			}
			f[i][j] = f[nxta[i][0]][nxtb[j][0]]+1;
			if(f[nxta[i][1]][nxtb[j][1]]+1<f[i][j]){
				f[i][j] = f[nxta[i][1]][nxtb[j][1]]+1;
				id[i][j] = 1;
			}else{
				id[i][j] = 0;
			}
//			f[i][j] = min(f[nxta[i][0]][nxtb[j][0]],f[nxta[i][1]][nxtb[j][1]])+1;
		}
	}
//	cout<<f[0][0]<<"\n";
	print(0,0);
	return 0;
}
/*
好像子序列自动机啊
草,还真是,见过类似的 
直接从上一个0/0或1/1转移,0/1显然不合法 
不考虑字典序做完了。。。。
捏麻麻的咋还有字典序啊。。。

哦傻白,转移的时候记录一下转移点,相同优先0就好了 
*/ 

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 0ms
memory: 1292kb

input:

011000100
0110110011

output:

000001

result:

ok single line: '000001'

Test #2:

score: 10
Accepted
time: 0ms
memory: 1288kb

input:

10111011
111000110

output:

0100

result:

ok single line: '0100'

Test #3:

score: 10
Accepted
time: 0ms
memory: 1956kb

input:

0110000001000011100110010010010110011100010111101000000110010011000101001101011011010100
11011000100...

output:

00110001110000110110111110000000010

result:

ok single line: '00110001110000110110111110000000010'

Test #4:

score: 10
Accepted
time: 0ms
memory: 1956kb

input:

011001000111110000100000011101000001011011100000000000001101000011110101001000111011001
000011100000...

output:

101001001100100011011100010000000

result:

ok single line: '101001001100100011011100010000000'

Test #5:

score: 10
Accepted
time: 0ms
memory: 1904kb

input:

011110011001111010001010110100110101001110001011100101101110011001001101101010010
101011111110101110...

output:

0000001100101100000110000010000000011

result:

ok single line: '0000001100101100000110000010000000011'

Test #6:

score: 10
Accepted
time: 48ms
memory: 78976kb

input:

0101100010010000110011110110100011111110010101101000101111101110010111101111000011100000101000101000...

output:

0001110000010000001000000010111011111100000101011111101011000000011100000001111110001111100010001111...

result:

ok single line: '000111000001000000100000001011...0000000000001001111000011001101'

Test #7:

score: 10
Accepted
time: 31ms
memory: 71820kb

input:

0101001011011101100111110110000010010010000001110111000000101010111110100000011101111011100010010000...

output:

0001110000011111100111101101001011110101000001111011111110011110111100111000000011001000000010011001...

result:

ok single line: '000111000001111110011110110100...0000100000001111110010000000000'

Test #8:

score: 10
Accepted
time: 41ms
memory: 72020kb

input:

1000111000101000001000110101101010101011111101000110000101101110110001101111100010100011001100010001...

output:

0010111100110011000111010000000000000000011001111110010001110001000111111010011110000000100000001100...

result:

ok single line: '001011110011001100011101000000...1111111000000011101001111111000'

Test #9:

score: 10
Accepted
time: 51ms
memory: 79528kb

input:

0000110111100010110111110010111001001110000100001111000111001001100000000010110111001110000001100000...

output:

1001000100011010101011010000010101110011111111110110011111000011000010000000000000000000000000011010...

result:

ok single line: '100100010001101010101101000001...0000000001000001011011001001001'

Test #10:

score: 10
Accepted
time: 42ms
memory: 77120kb

input:

0010010010000000101101000100111010010010010001101000110000110101110111010001010100011000110000001001...

output:

0111100011001111001010000011111101101000110110000010100000011000111110111001100000011110000011000000...

result:

ok single line: '011110001100111100101000001111...1001110001000011001000000000000'