Cod sursă (job #296039)

Utilizator avatar LucianT Trepteanu Lucian LucianT IP ascuns
Problemă Ssdj (lot liceu) Compilator cpp | 1,34 kb
Rundă Arhiva de probleme Status evaluat
Dată 27 mar. 2017 22:40:57 Scor 100
#include <bits/stdc++.h>
#define maxN 1005
using namespace std;
int n,i,j,sol;
char a[maxN][maxN];
int stk[maxN],top,_right[maxN];
int up[maxN][maxN];
int main()
{
    freopen("ssdj.in","r",stdin);
    freopen("ssdj.out","w",stdout);
    scanf("%d\n",&n);
    for(i=1;i<=n;i++)
        gets(a[i]+1);

    for(char ch='a';ch<='z';ch++)
    {
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
                if(a[i][j]>=ch)
                    up[i][j]=0;
                else up[i][j]=1;

        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
                if(up[i][j])
                    up[i][j]=up[i-1][j]+1;

        for(i=2;i<=n;i++)
        {
            top=0;
            for(j=1;j<=n;j++)
                if(up[i][j]>0)
                {
                    while(top>0 && stk[top]>=up[i][j])
                        top--;
                    stk[++top]=up[i][j];
                    _right[top]=j;
                }
                else
                {
                    while(top>0 && stk[top]>=up[i-1][j]+1)
                        top--;
                    for(int k=1;k<=top;k++)
                        if(a[i][j]==ch || a[i-stk[k]][_right[k]]==ch)
                            sol++;
                    top=0;
                }
        }
    }
    printf("%d",sol);
    return 0;
}