http://codeforces.com/contest/253/problem/D
挺麻烦的一道题
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<map> #include<vector> #include<stack> #include<queue> #include<algorithm> #define LL long long using namespace std; const int INF=0x3f3f3f3f; const int N=405; int sum[N][N]; char s[N][N]; int level[N]; int n,m,k; LL ans; bool OK(int x1,int y1,int x2,int y2) { if(x1==x2) return true; int tmp=sum[x2][y2]-sum[x2][y1-1]-sum[x1-1][y2]+sum[x1-1][y1-1]; if(tmp<=k) return true; return false; } void Fsearch(char w) {//cout<<w<<endl; for(int j1=1;j1<=m;++j1) for(int j2=j1+1;j2<=m;++j2) { int I=0; for(int i=1;i<=n;++i) { if(s[i][j1]==w&&s[i][j2]==w) level[I++]=i; } int l=0; int r=1; while(r<I) { if(OK(level[l],j1,level[r],j2)) { ans+=(r-l); ++r; } else { ++l; } } } } int main() { //freopen("data.in","r",stdin); freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); while(scanf("%d %d %d",&n,&m,&k)!=EOF) { getchar(); memset(sum,0,sizeof(sum)); bool had[27]; memset(had,false,sizeof(had)); for(int i=1;i<=n;++i) gets(s[i]+1); for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) { had[s[i][j]-'a']=true; if(s[i][j]=='a') sum[i][j]=1; } for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) { sum[i][j]+=(sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]); } ans=0; for(char w='a';w<='z';++w) if(had[w-'a']) Fsearch(w); printf("%I64d\n",ans); } return 0; }