CSES - Datatähti 2025 alku - Results
Submission details
Task:Niitty
Sender:maweiyin24562
Submission time:2024-11-02 23:14:47 +0200
Language:C++ (C++11)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:61:37: error: 'tag' was not declared in this scope; did you mean 'tan'?
   61 |                                 if(!tag)if(check(top,l,btm,r))tag=true;
      |                                     ^~~
      |                                     tan
input/code.cpp:62:36: error: 'tag' was not declared in this scope; did you mean 'tan'?
   62 |                                 if(tag)ans+=l;
      |                                    ^~~
      |                                    tan
input/code.cpp:27:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |         scanf("%d",&n);
      |         ~~~~~^~~~~~~~~

Code

#include <bits/stdc++.h>

using namespace std;

#define IC(c) (int)(c-'A')

#define sum(x1,y1,x2,y2,id) pre[x2][y2][id]-pre[x1-1][y2][id]-pre[x2][y1-1][id]+pre[x1-1][y1-1][id]
const int N=509;
const int F=26;
int n,C;

int pre[N][N][F+1];
//pre[i][j][c] : prefix sum
//compact[j][c] for every array[top,btm]
//the compacted prefix sum
int idx[F+1];//char-int list

bool check(int x1,int y1,int x2,int y2){
	bool T=1;
	for(int i=1;i<=C;i++)T=T&&sum(x1,y1,x2,y2,i);
	return T;
}
int ans;
//sum up answer

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++){
			char c;
			cin>>c;
			int id=IC(c);
			if(!idx[id]){
				C++;
				idx[id]=C;
			}
			for(int k=1;k<=C;k++){
				if(k==idx[id])
					pre[i][j][k]=pre[i-1][j][k]+pre[i][j-1][k]-pre[i-1][j-1][k]+1;
				else
					pre[i][j][k]=pre[i-1][j][k]+pre[i][j-1][k]-pre[i-1][j-1][k];
			}
		}
	//input ends here
	//for every [top,btm] in [1,n]
	for(int top=1;top<=n;top++){
		for(int btm=top;btm<=n;btm++){//O(n^2)
			if(!check(top,1,btm,n)){
				if(btm==n){
					cout<<ans<<endl;
					return 0;
				}
				else continue;
			}
			int l=1,r=1;
			//scan the array
			while(r<=n){//O(n)
				while(check(top,l+1,btm,r)&&l<r){
					l++;
				}
				if(!tag)if(check(top,l,btm,r))tag=true;
				if(tag)ans+=l;
				r++;
			}
		}
	}

	printf("%d\n",ans);
	return 0;
}