菜鸡的代码有着奇怪的WA
查看原帖
菜鸡的代码有着奇怪的WA
510555
ImposterAnYu楼主2023/6/24 20:51
#include<bits/stdc++.h>
#define N 500000
using namespace std;
int n,m,i,j,k,l,r,mid,a,b,mm;
char s[N + 5],t[N + 5],ans[N + 5],ans2[N + 5];
void C(){//关同步。 
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	return ;
}
bool check(int k){//二分的check函数,判断字符串t的前k个字符是否等于后t个字符。 
	for(int i = 1,j = m - k + 1; i <= k; i++,j++){
		if(t[i] != t[j]){
			return 0;
		}
	}
	return 1;
}
int main(){
	C();
	cin >> s + 1 >> t + 1;
	n = strlen(s + 1),m = strlen(t + 1);
	for(i = 1; i <= n; i++){//计算s中0和1的个数。 
		if(s[i] == '0'){
			a++;
		}else{
			b++;
		}
	}
	l = 0,r = m;
	while(l < r){//为了与众不同没用KMP(主要是忘了)求nxt,用的二分。 
		mid = (l + r) >> 1;
		if(mid == l){//防死循环。 
			break;
		}
		if(check(mid)){
			l = mid;
		}else{
			r = mid - 1;
		}
	}
	mm = m - l;//此时的l就是nxt[m]。 
	if(l){ 
		for(i = 1; i <= l; i++){//ans为前缀和后缀的公共部分。 
			ans[i] = t[mm + i];
		}
		for(i = 1; i <= mm; i++){
			if(t[i] == '0'){
				if(a){
					ans2[i] = '0',a--;
				}else{//如果不能让t再次出现,就把剩下的字符一股脑堆在后面。 
					cout<< ans2 + 1 ;
					for(j = i; j <= n; j++){
						cout<< '1' ;
					}
					return 0;
				}
			}else{
				if(b){
					ans2[i] = '1',b--;
				}else{//同上。 
					cout<< ans2 + 1 ;
					for(j = i; j <= n; j++){
						cout<< '0' ;
					}
					return 0;
				}
			}
		}
		for(i = mm + 1,k = 1; i <= n; i++,k++){ 
			if(t[k] == '0'){
				if(a){
					ans2[i] = '0',a--;
				}else{
					cout<< ans2 + 1 ;
					for(j = i; j <= n; j++){
						cout<< '1' ;
					}
					return 0;
				}
			}else{
				if(b){
					ans2[i] = '1',b--;
				}else{
					cout<< ans2 + 1 ;
					for(j = i; j <= n; j++){
						cout<< '0' ;
					}
					return 0;
				}
			}
			if(k == l){
				k = 0;
			}
		}
		cout<< ans2 + 1 ;
	}else{
		for(i = 1,k = 1; i <= n; i++,k++){//如果前后缀没有公共部分,默认这个公共部分为t。 
			if(t[k] == '0'){
				if(a){
					ans2[i] = '0',a--;
				}else{
					cout<< ans2 + 1 ;
					for(j = i; j <= n; j++){
						cout<< '1' ;
					}
					return 0;
				}
			}else{
				if(b){
					ans2[i] = '1',b--;
				}else{
					cout<< ans2 + 1 ;
					for(j = i; j <= n; j++){
						cout<< '0' ;
					}
					return 0;
				}
			}
			if(k == m){
				k = 0;
			}
		}
		cout<< ans2 + 1 ;
	}
	return 0;
}

结果:WA #6(返回信息:Wrong Answer.wrong answer jury has more occurences: 1 (participant) vs 2 (jury))

2023/6/24 20:51
加载中...