Pagini recente » Teorema chineza a resturilor - generalizari si aplicatii | Cod sursa (job #1625859) | Cod sursa (job #67119) | Cod sursa (job #2550439) | Cod sursa (job #1827788)
#include <bits/stdc++.h>
#define nrmax 1007
using namespace std;
ifstream fin ("matrix.in");
ofstream fout ("matrix.out");
char a[nrmax][nrmax], b[nrmax][nrmax];
long long sum[nrmax][nrmax], sum1[nrmax][nrmax], n, m;
void Citire()
{
int i;
fin >> n >>m ;
for (i=1; i<=n; i++)
fin >> (a[i]+1);
for (i=1; i<=m; i++)
fin >> (b[i]+1);
}
void Rezolvare()
{
long long s=0, spatrat=0, x, t, t1;
int i, j, sol=0;
for (i=1; i<=m; i++)
for (j=1; j<=m; j++)
{
x=b[i][j]-'a'+1;
s+=x;
spatrat+=x*x;
}
for (i=1; i<=n; i++)
for (j=1; j<=n; j++)
{
x=a[i][j]-'a'+1;
sum[i][j]=x+sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
x=x*x;
sum1[i][j]=x+sum1[i-1][j]+sum1[i][j-1]-sum1[i-1][j-1];
}
for (i=m; i<=n; i++)
for (j=m; j<=n; j++)
{
t=sum[i][j]-sum[i-m][j]-sum[i][j-m]+sum[i-m][j-m];
t1=sum1[i][j]-sum1[i-m][j]-sum1[i][j-m]+sum1[i-m][j-m];
if (t==s && t1==spatrat)
sol++;
}
fout << sol << "\n";
}
int main()
{
Citire();
Rezolvare();
fin.close();
fout.close();
return 0;
}