【悬2关】死循环代码求调
查看原帖
【悬2关】死循环代码求调
373198
WhileTrueRP楼主2024/3/31 17:26

样例一死循环。

#include<bits/stdc++.h>
using namespace std;
const int N = 2e3+5;
int a[2][N][30];
bool vis[N][N];
string s[N];
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++){
		cin>>s[i];
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			a[0][i][s[i][j]-'a'] ++;
			a[1][j][s[i][j]-'a'] ++;
		}
	}
	while(true){
		bool flag = true;
		for(int i=0;i<n;i++){
			int cnt = 0,x = 0;;
			for(int j=0;j<=26;j++){
				if(a[0][i][j] != 0){
					cnt ++;
					x = j;
				}
			}
			if(cnt == 1){
				flag = false;
				a[0][i][x] = 0;
				for(int j=0;j<m;j++){
					if(!vis[i][j]){
						vis[i][j] = true;
						a[1][j][x] --;
					}
				}
			}
		}
		for(int i=0;i<m;i++){
			int cnt = 0,x = 0;
			for(int j=0;j<=26;j++){
				if(a[1][i][j] != 0){
					cnt ++;
					x = j;
				}
			}
			if(cnt == 1){
				flag =  false;
				a[0][i][x] = 0;
				for(int j=0;j<n;j++){
					if(!vis[i][j]){
						vis[i][j] = true;
						a[0][j][x] --;
					}
				}
			}
		}
		if(flag == true){
			break;
		}
//		cout<<flag;
	}
	int ans = 0;
	for(int i=0;i<n;i++){
		for(int j=0;j<=26;j++){
			ans += a[0][i][j];
		}
	}
	printf("%d",ans);
}
2024/3/31 17:26
加载中...