Submission #1329455

#TimeUsernameProblemLanguageResultExecution timeMemory
1329455Jawad_Akbar_JJTrener (COCI20_trener)C++20
110 / 110
104 ms75668 KiB
#include <iostream>
#include <map>
#include <set>

using namespace std;
#define int long long
const int N = 1505, mod = 1e9 + 7, C = 256;
int dp[55][N], Ans;
string a[N][N];

map<int, int> mp, mp2;
signed main(){
	int n, k;
	cin>>n>>k;

	for (int i=1;i<=n;i++){
		for (int j=1;j<=k;j++)
			cin>>a[i][j];
	}

	for (int i=n;i>=1;i--){
		for (int j=1;j<=k;j++){
			int h1 = 0, h2 = 0, h3 = 0;
			for (int l=1;l<=i;l++){
				h1 = (h1 * C + a[i][j][l-1]) % mod;
				if (l != i)
					h2 = (h2 * C + a[i][j][l-1]) % mod;
				if (l != 1)
					h3 = (h3 * C + a[i][j][l-1]) % mod;
			}
			if (i == n)
				dp[i][j] = 1;
			else
				dp[i][j] = mp[h1] % mod;

			mp2[h2] += dp[i][j];
			mp2[h3] += dp[i][j] * (h2 != h3);

			Ans += dp[1][j];
		}
		mp.clear();
		swap(mp2, mp);
	}
	cout<<Ans % mod<<'\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...