http://acm.timus.ru/problem.aspx?space=1&num=1287
我说Timus 你还能再水一些吗 递推 + 滚动数组
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> #define LL long long //#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const int N=1500; char s[N][N]; struct node { int ud,lr,k1,k2; }ans[2][N]; int Snum,snum; int Fmax4(int a,int b,int c,int d) { if(a<b) a=b; if(a<c) a=c; if(a<d) a=d; return a; } int main() { //freopen("data.txt","r",stdin); int n; while(scanf("%d",&n)!=EOF) { memset(s,0,sizeof(s)); for(int i=1;i<=n;++i) { getchar(); for(int j=1;j<=n;++j) { scanf("%c",&s[i][j]); } } Snum=0,snum=0; for(int i=1;i<=n;++i) { int l=i%2; int l1=(i-1)%2; for(int j=1;j<=n;++j) { ans[l][j].k1=1; ans[l][j].ud=1; ans[l][j].lr=1; if(s[i][j]==s[i-1][j]) { ans[l][j].ud=ans[l1][j].ud+1; } if(s[i][j]==s[i][j-1]) { ans[l][j].lr=ans[l][j-1].lr+1; } if(s[i][j]==s[i-1][j-1]) { ans[l][j].k1=ans[l1][j-1].k1+1; } if(s[i][j]=='S') Snum=Fmax4(Snum,ans[l][j].lr,ans[l][j].ud,ans[l][j].k1); else snum=Fmax4(snum,ans[l][j].lr,ans[l][j].ud,ans[l][j].k1); } for(int j=n;j>=1;--j) { ans[l][j].k2=1; if(s[i][j]==s[i-1][j+1]) { ans[l][j].k2=ans[l1][j+1].k2+1; } if(s[i][j]=='S') Snum=max(Snum,ans[l][j].k2); else snum=max(snum,ans[l][j].k2); } } //cout<<Snum<<" "<<snum<<endl; if(Snum>snum) { printf("S\n%d\n",Snum); }else if(snum>Snum) { printf("s\n%d\n",snum); }else { printf("?\n%d",snum); } } return 0; }