Logo Wy Online Judge

WyOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#506#106. 「NOIP2024」编辑字符串xuyunao1001449ms11372kbC++142.1kb2025-04-29 21:06:472025-04-29 21:07:59

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int maxn = 1e5 + 10;
string s1,s2;
int cnt[2][maxn][2];//记录 s1 和 s2 中的各个数的数量
int fa[2][maxn];
int s[2][maxn];
int t[2][maxn];
int n;
void init()
{
	for(int i = 0;i <= 1;i++)
	{
		for(int j = 1;j <= n;j++)
		{
			fa[i][j] = j;
			cnt[i][j][0] = cnt[i][j][1] = 0;
			s[i][j] = t[i][j] = 0;
		}
	}
}

int getf(int num,int x)
{
	if(fa[num][x] == x) return x;
	return fa[num][x] = getf(num,fa[num][x]);
}

void merge(int num,int x,int y)
{
	x = getf(num,x);
	y = getf(num,y);
	if(x == y) return;
	fa[num][x] = y;
	cnt[num][y][0] += cnt[num][x][0];
	cnt[num][y][1] += cnt[num][x][1];
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int T;
	cin >> T;
	while(T--)
	{
		cin >> n;
		init();
		char ch;
		for(int i = 0;i <= 1;i++)
		{
			for(int j = 1;j <= n;j++)
			{
				cin >> ch;
				s[i][j] = (ch == '1');
				if(s[i][j]) cnt[i][j][1] = 1;
				else cnt[i][j][0] = 1;
			}
		}
		
		for(int i = 0;i <= 1;i++)
		{
			for(int j = 1;j <= n;j++)
			{
				cin >> ch;
				t[i][j] = (ch == '1');
				if(t[i][j] == t[i][j-1] && t[i][j] == 1)
				{
					merge(i,j,j-1);
				}
			}
		}
		
		
//		for(int i = 0;i <= 1;i++)
//		{
//			cout << endl;
//			for(int j = 1;j <= n;j++)
//			{
//				cout << getf(i,j) << " ";
//			}
//		}
//		cout << endl;
		
		int ans = 0;
	    for (int num = 1;num <= n;num++)
	    {
	    	int i = getf(0,num);
	    	int j = getf(1,num);
	        if (cnt[0][i][0] > 0 && cnt[1][j][0] > 0)
	        {
	        	ans++; 
				cnt[0][i][0]--;
				cnt[1][j][0]--;
			}
	        else if(cnt[0][i][1] > 0 && cnt[1][j][1] > 0)
			{
	        	ans++; 
				cnt[0][i][1]--;
				cnt[1][j][1]--;
			}
	        else
	        {
	            if (cnt[0][i][0] > 0) 
				{
					cnt[0][i][0]--;
					cnt[1][j][1]--;
				}
	            else 
	            {
					cnt[0][i][1]--;
					cnt[1][j][0]--;
				}
	        }
	    }
	    cout << ans << endl;
	}
	return 0;
}

详细

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

Test #1:

score: 5
Accepted
time: 3ms
memory: 9580kb

input:

10
9
111101010
101011100
101111111
111111011
9
001110100
111010101
111111111
011011111
8
01000100
00...

output:

8
7
6
9
4
5
4
4
4
7

result:

ok 10 tokens

Test #2:

score: 5
Accepted
time: 2ms
memory: 9508kb

input:

10
10
0110100110
0100000010
1011011011
0101110111
7
0100110
0001100
0011111
1111111
8
00000000
11111...

output:

7
6
0
4
4
2
7
7
8
6

result:

ok 10 tokens

Test #3:

score: 5
Accepted
time: 0ms
memory: 9576kb

input:

10
10
1001000001
0100000011
1000101101
0011111100
9
111111100
000000000
111001010
100011011
9
111111...

output:

6
2
5
0
10
2
7
4
8
3

result:

ok 10 tokens

Test #4:

score: 5
Accepted
time: 1ms
memory: 9392kb

input:

10
10
1011011111
0000000000
1111100010
0001011111
9
000000000
000100100
111111111
000111110
7
111011...

output:

2
7
3
4
8
2
3
5
4
7

result:

ok 10 tokens

Test #5:

score: 5
Accepted
time: 8ms
memory: 7512kb

input:

10
999
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

output:

405
765
180
993
368
465
562
104
471
287

result:

ok 10 tokens

Test #6:

score: 5
Accepted
time: 8ms
memory: 9376kb

input:

10
998
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

output:

514
504
541
518
598
337
108
445
320
533

result:

ok 10 tokens

Test #7:

score: 5
Accepted
time: 167ms
memory: 11052kb

input:

10
100000
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

output:

975
48188
43831
36011
48402
12101
68446
26937
14167
37132

result:

ok 10 tokens

Test #8:

score: 5
Accepted
time: 171ms
memory: 11180kb

input:

10
99999
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

output:

32254
46821
36505
43488
59340
47604
54524
53897
53675
25677

result:

ok 10 tokens

Test #9:

score: 5
Accepted
time: 4ms
memory: 9592kb

input:

10
1000
00000001001000000000001011000011100000010100011001101000100000000000000000000000000000000011...

output:

463
511
423
412
535
553
828
587
512
520

result:

ok 10 tokens

Test #10:

score: 5
Accepted
time: 4ms
memory: 7580kb

input:

10
997
111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...

output:

534
569
588
635
623
582
479
510
656
402

result:

ok 10 tokens

Test #11:

score: 5
Accepted
time: 174ms
memory: 11368kb

input:

10
100000
111011111110110111111111010001011110111001101111010101010101111011111110011111011111111101...

output:

59099
60314
62231
58265
75929
36728
58446
62981
59041
19224

result:

ok 10 tokens

Test #12:

score: 5
Accepted
time: 175ms
memory: 11372kb

input:

10
99999
0111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110...

output:

50503
60559
64431
51759
55159
78816
88085
57698
65174
91077

result:

ok 10 tokens

Test #13:

score: 5
Accepted
time: 2ms
memory: 9448kb

input:

10
997
100000001000000010100000110000010010100011000100000000010001000100011010010001001100001000000...

output:

477
829
592
573
614
132
931
610
980
237

result:

ok 10 tokens

Test #14:

score: 5
Accepted
time: 9ms
memory: 9604kb

input:

10
999
000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000100...

output:

503
242
975
885
750
528
896
389
387
258

result:

ok 10 tokens

Test #15:

score: 5
Accepted
time: 175ms
memory: 11372kb

input:

10
100000
101111010011110111110101110111111001001011011001111111011111011111001111001111010110111011...

output:

47943
42512
86300
81321
81110
69327
48115
24200
30695
54529

result:

ok 10 tokens

Test #16:

score: 5
Accepted
time: 176ms
memory: 11052kb

input:

10
99998
1111111111111111111111111111111111111111101111111111111111111111111111111111111111111111111...

output:

87231
36605
52504
24563
45410
86215
72694
26325
39997
59428

result:

ok 10 tokens

Test #17:

score: 5
Accepted
time: 4ms
memory: 9596kb

input:

10
999
000100010000110000010000100101010000010001100001100101001010001000110111100100000011000111001...

output:

776
646
705
452
759
130
699
560
427
614

result:

ok 10 tokens

Test #18:

score: 5
Accepted
time: 8ms
memory: 9788kb

input:

10
998
101000000000000000001000000010000000000000000000000010000001010101000000000010000000000100100...

output:

472
616
691
649
567
307
170
748
727
701

result:

ok 10 tokens

Test #19:

score: 5
Accepted
time: 178ms
memory: 11096kb

input:

10
99999
0000000100000000001000100000001000000000000000000000000001000000000000000000011000000000000...

output:

70833
64573
51649
93023
74958
62637
67006
80120
76759
63472

result:

ok 10 tokens

Test #20:

score: 5
Accepted
time: 180ms
memory: 11252kb

input:

10
99999
0000001101101101001111001111000111111000111101111111111011110010111111011101010111101010101...

output:

59680
67434
35099
69290
59184
81406
64316
50750
65959
42615

result:

ok 10 tokens