QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#665601#9514. 研心Kevin5307100 ✓4444ms236392kbC++239.6kb2024-10-22 14:21:082024-10-22 14:21:10

Judging History

你现在查看的是最新测评结果

  • [2024-10-22 14:21:10]
  • 评测
  • 测评结果:100
  • 用时:4444ms
  • 内存:236392kb
  • [2024-10-22 14:21:08]
  • 提交

answer

//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
const int maxn=800800;
const int thres=300;
namespace PAM
{
	string s;
	int trans[maxn][26];
	int len[maxn],fail[maxn],fa[maxn],tot,last;
	int dp[maxn];
	int len2[maxn];
	vector<int> vlast;
	vector<pii> vop;
	void clear()
	{
		for(int i=1;i<=tot;i++)
		{
			len[i]=fail[i]=fa[i]=0;
			len2[i]=0;
			memset(trans[i],0,sizeof(trans[i]));
		}
		tot=2;
		len[1]=0;
		len[2]=-1;
		fail[1]=fa[1]=2;
		fail[2]=fa[2]=1;
		last=2;
		vlast.clear();
		vlast.pb(2);
		vop.clear();
		s="";
	}
	vector<int> build(string S)
	{
		int n=S.length();
		vector<int> res(n);
		for(int i=0;i<n;i++)
		{
			while(len[last]==i||S[i-len[last]-1]!=S[i]) last=fail[last];
			if(!trans[last][S[i]-'a'])
			{
				tot++;
				fa[tot]=last;
				len[tot]=len[last]+2;
				int p=fail[last];
				while(S[i-len[p]-1]!=S[i])
					p=fail[p];
				fail[tot]=trans[p][S[i]-'a'];
				if(!fail[tot]) fail[tot]=1;
				trans[last][S[i]-'a']=tot;
			}
			res[i]=last=trans[last][S[i]-'a'];
		}
		return res;
	}
	int append(char ch)
	{
		s+=ch;
		int i=sz(s)-1;
		while(len[last]==i||s[i-len[last]-1]!=s[i]) last=fail[last];
		if(!trans[last][s[i]-'a'])
		{
			tot++;
			fa[tot]=last;
			len[tot]=len[last]+2;
			int p=fail[last];
			while(s[i-len[p]-1]!=s[i])
				p=fail[p];
			fail[tot]=trans[p][s[i]-'a'];
			if(!fail[tot]) fail[tot]=1;
			trans[last][s[i]-'a']=tot;
			vop.pb(last,s[i]-'a');
			len2[tot]=(len[tot]&1)?len[tot]:len2[fail[tot]];
		}
		last=trans[last][s[i]-'a'];
		vlast.pb(last);
		return len2[last];
	}
	void calc()
	{
		dp[1]=dp[2]=-1;
		for(int i=3;i<=tot;i++)
			dp[i]=1+max(dp[fail[i]],dp[fa[i]]);
	}
}
int n,m;
string s[maxn],t[maxn];
namespace Solver
{
	int n,m;
	string s[maxn],t[maxn];
	int sv[maxn],tv[maxn];
	int get(string s)
	{
		int mx=0;
		for(int i=0;i<sz(s);i++)
		{
			int ans=1;
			while(ans<=i&&i+ans<sz(s)&&s[i-ans]==s[i+ans]) ans++;
			mx=max(mx,ans);
		}
		return mx;
	}
	int t1[maxn][26],t2[maxn][26];
	int tot1=1,tot2=1;
	int f1[maxn],fe1[maxn],f2[maxn],fe2[maxn];
	vector<int> ind1[maxn],ind2[maxn];
	vector<pii> v1[maxn],v2[maxn];
	int dfn1[maxn],dfn2[maxn];
	int in1[maxn],out1[maxn],in2[maxn],out2[maxn];
	int cnt1,cnt2;
	int d1[maxn],d2[maxn];
	void dfs1(int x)
	{
		in1[x]=cnt1+1;
		if(sz(ind1[x]))
		{
			for(auto ind:ind1[x])
				dfn1[ind]=++cnt1;
		}
		for(int i=0;i<26;i++)
			if(t1[x][i])
				dfs1(t1[x][i]);
		out1[x]=cnt1;
	}
	void dfs2(int x)
	{
		in2[x]=cnt2+1;
		if(sz(ind2[x]))
		{
			for(auto ind:ind2[x])
				dfn2[ind]=++cnt2;
		}
		for(int i=0;i<26;i++)
			if(t2[x][i])
				dfs2(t2[x][i]);
		out2[x]=cnt2;
	}
	vector<int> pool1,pool2;
	string curs;
	void check1(int x,int len)
	{
		if(sz(curs)>=len*2) return ;
		if(sz(curs)>=len)
		{
			int need=len+len-1-sz(curs);
			string ss=curs.substr(0,sz(curs)-need);
			string tt=ss;
			rev(tt);
			if(ss==tt)
			{
				int nd=1;
				for(int i=sz(curs)-need;i<sz(curs);i++)
					nd=t2[nd][curs[i]-'a'];
				int L1=in1[x],R1=out1[x];
				int L2=in2[nd],R2=out2[nd];
				L1=lb(pool1,L1)+1;
				R1=ub(pool1,R1);
				L2=lb(pool2,L2)+1;
				R2=ub(pool2,R2);
				if(L1<=R1&&L2<=R2)
				{
					v1[L1].pb(L2,R2);
					v2[R1].pb(L2,R2);
				}
			}
		}
		for(int i=0;i<26;i++)
			if(t1[x][i])
			{
				curs+=(char)('a'+i);
				check1(t1[x][i],len);
				curs.pop_back();
			}
	}
	void check2(int x,int len)
	{
		if(sz(curs)>=len*2) return ;
		if(sz(curs)>=len)
		{
			int need=len+len-1-sz(curs);
			string ss=curs.substr(0,sz(curs)-need);
			string tt=ss;
			rev(tt);
			if(ss==tt)
			{
				int nd=1;
				for(int i=sz(curs)-need;i<sz(curs);i++)
					nd=t1[nd][curs[i]-'a'];
				int L1=in1[nd],R1=out1[nd];
				int L2=in2[x],R2=out2[x];
				L1=lb(pool1,L1)+1;
				R1=ub(pool1,R1);
				L2=lb(pool2,L2)+1;
				R2=ub(pool2,R2);
				if(L1<=R1&&L2<=R2)
				{
					v1[L1].pb(L2,R2);
					v2[R1].pb(L2,R2);
				}
			}
		}
		for(int i=0;i<26;i++)
			if(t2[x][i])
			{
				curs+=(char)('a'+i);
				check2(t2[x][i],len);
				curs.pop_back();
			}
	}
	void check12(int a,int b,int len)
	{
		if(!len)
		{
			int L1=in1[a],R1=out1[a];
			int L2=in2[b],R2=out2[b];
			L1=lb(pool1,L1)+1;
			R1=ub(pool1,R1);
			L2=lb(pool2,L2)+1;
			R2=ub(pool2,R2);
			if(L1<=R1&&L2<=R2)
			{
				v1[L1].pb(L2,R2);
				v2[R1].pb(L2,R2);
			}
			return ;
		}
		for(int i=0;i<26;i++)
			if(t1[a][i]&&t2[b][i])
				check12(t1[a][i],t2[b][i],len-1);
	}
	void check11(int a,int b,int len)
	{
		if(!a||!b) return ;
		if(!len) return ;
		if(a==1)
		{
			check12(b,a,len);
			return ;
		}
		int x=fe1[a];
		check11(f1[a],t1[b][x],len-1);
	}
	void check22(int a,int b,int len)
	{
		if(!a||!b) return ;
		if(!len) return ;
		if(a==1)
		{
			check12(a,b,len);
			return ;
		}
		int x=fe2[a];
		check22(f2[a],t2[b][x],len-1);
	}
	int mn[maxn<<2],tag[maxn<<2],cnt[maxn<<2];
	#define ls (x<<1)
	#define rs (ls|1)
	void build(int x,int l,int r)
	{
		mn[x]=tag[x]=0;
		cnt[x]=r-l+1;
		if(l==r) return ;
		int mid=(l+r)/2;
		build(ls,l,mid);
		build(rs,mid+1,r);
	}
	void update(int x,int l,int r,int ql,int qr,int v)
	{
		if(ql<=l&&r<=qr)
		{
			tag[x]+=v;
			mn[x]+=v;
			return ;
		}
		int mid=(l+r)/2;
		if(ql<=mid) update(ls,l,mid,ql,qr,v);
		if(qr>mid) update(rs,mid+1,r,ql,qr,v);
		mn[x]=tag[x]+min(mn[ls],mn[rs]);
		cnt[x]=0;
		if(mn[ls]<=mn[rs]) cnt[x]+=cnt[ls];
		if(mn[rs]<=mn[ls]) cnt[x]+=cnt[rs];
	}
	ll Main()
	{
		for(int i=1;i<=n;i++)
			sv[i]=get(s[i]);
		for(int i=1;i<=m;i++)
			tv[i]=get(t[i]);
		for(int i=1;i<=n;i++)
		{
			int cur=1;
			for(int j=sz(s[i])-1;j>=0;j--)
			{
				int x=s[i][j]-'a';
				if(!t1[cur][x])
				{
					t1[cur][x]=++tot1;
					f1[tot1]=cur;
					fe1[tot1]=x;
					d1[tot1]=d1[cur]+1;
				}
				cur=t1[cur][x];
			}
			ind1[cur].pb(i);
		}
		for(int i=1;i<=m;i++)
		{
			int cur=1;
			for(auto ch:t[i])
			{
				int x=ch-'a';
				if(!t2[cur][x])
				{
					t2[cur][x]=++tot2;
					f2[tot2]=cur;
					fe2[tot2]=x;
					d2[tot2]=d2[cur]+1;
				}
				cur=t2[cur][x];
			}
			ind2[cur].pb(i);
		}
		dfs1(1);
		dfs2(1);
		ll ans=0;
		for(int i=1;i<=thres;i++)
		{
			ll tmp=ans;
			pool1.clear();
			pool2.clear();
			for(int j=1;j<=n;j++)
				if(sv[j]<i)
					pool1.pb(dfn1[j]);
			for(int j=1;j<=m;j++)
				if(tv[j]<i)
					pool2.pb(dfn2[j]);
			srt(pool1);
			srt(pool2);
			ans+=1ll*n*m-1ll*sz(pool1)*sz(pool2);
			if(!sz(pool1)||!sz(pool2)) continue;
			for(int j=1;j<=n;j++)
			{
				v1[j].clear();
				v2[j].clear();
			}
			for(int j=2;j<=tot1;j++)
				if(d1[j]<i)
					check11(f1[j],j,i-1);
			for(int j=2;j<=tot2;j++)
				if(d2[j]<i)
					check22(f2[j],j,i-1);
			build(1,1,sz(pool2));
			for(int j=1;j<=sz(pool1);j++)
			{
				for(auto pr:v1[j])
					update(1,1,sz(pool2),pr.first,pr.second,1);
				int c=(mn[1]==0?cnt[1]:0);
				ans+=sz(pool2)-c;
				for(auto pr:v2[j])
					update(1,1,sz(pool2),pr.first,pr.second,-1);
			}
			if(tmp==ans) break;
		}
		return ans;
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		cin>>s[i];
	for(int i=1;i<=m;i++)
		cin>>t[i];
	ll ans=0;
	for(int i=1;i<=n;i++)
		if(sz(s[i])<=thres)
			Solver::s[++Solver::n]=s[i];
	for(int i=1;i<=m;i++)
		if(sz(t[i])<=thres)
			Solver::t[++Solver::m]=t[i];
	ans=Solver::Main();
	for(int i=1;i<=n;i++) if(sz(s[i])>thres)
	{
		int l=0;
		PAM::clear();
		for(auto ch:s[i])
			l=max(l,PAM::append(ch));
		int ct=PAM::tot;
		int cl=sz(PAM::vop);
		int cs=sz(PAM::vlast);
		for(int j=1;j<=m;j++)
		{
			int tmp=l;
			int rem=sz(t[j]);
			for(auto ch:t[j])
			{
				rem--;
				int cur=PAM::append(ch);
				if(cur+rem+rem<=tmp) break;
				tmp=max(tmp,cur);
			}
			ans+=(tmp+1)/2;
			for(int i=ct+1;i<=PAM::tot;i++)
				PAM::len[i]=PAM::fail[i]=PAM::fa[i]=PAM::len2[i]=0;
			PAM::tot=ct;
			while(sz(PAM::vop)>cl)
			{
				auto pr=PAM::vop.back();
				PAM::trans[pr.first][pr.second]=0;
				PAM::vop.pop_back();
			}
			while(sz(PAM::vlast)>cs)
				PAM::vlast.pop_back();
			PAM::last=PAM::vlast.back();
			while(sz(PAM::s)>sz(s[i]))
				PAM::s.pop_back();
		}
	}
	for(int i=1;i<=m;i++) if(sz(t[i])>thres)
	{
		int l=0;
		PAM::clear();
		for(int p=sz(t[i])-1;p>=0;p--)
			l=max(l,PAM::append(t[i][p]));
		int ct=PAM::tot;
		int cl=sz(PAM::vop);
		int cs=sz(PAM::vlast);
		for(int j=1;j<=n;j++) if(sz(s[j])<=thres)
		{
			int tmp=l;
			int rem=sz(s[j]);
			for(int p=sz(s[j])-1;p>=0;p--)
			{
				rem--;
				int cur=PAM::append(s[j][p]);
				if(cur+rem+rem<=tmp) break;
				tmp=max(tmp,cur);
			}
			ans+=(tmp+1)/2;
			for(int i=ct+1;i<=PAM::tot;i++)
				PAM::len[i]=PAM::fail[i]=PAM::fa[i]=PAM::len2[i]=0;
			PAM::tot=ct;
			while(sz(PAM::vop)>cl)
			{
				auto pr=PAM::vop.back();
				PAM::trans[pr.first][pr.second]=0;
				PAM::vop.pop_back();
			}
			while(sz(PAM::vlast)>cs)
				PAM::vlast.pop_back();
			PAM::last=PAM::vlast.back();
			while(sz(PAM::s)>sz(t[i]))
				PAM::s.pop_back();
		}
	}
	cout<<ans<<'\n';
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 20
Accepted

Test #1:

score: 20
Accepted
time: 7ms
memory: 137608kb

input:

10 100
aabaababaaaababbabaaaababaababbbaabbbaabaabaabbbaababaaababbaababababbbaababbbbbbbbaabbbbaabbbbbbbbaabaabbbaabbbbbabbbbbaabbbaabaa
bbaaabaabbbbaaabaaaaabbbbbabaabbabbaabbaabbaabbabbababaabaabbaabbbbbbaaaabbbbabaaaabaaabbbabaaaaaabbaaaabbbabbbbbaaaabbabaababbaabbaaabbbbbbaaaabbbababaabbaaaba
b...

output:

10468

result:

ok single line: '10468'

Test #2:

score: 20
Accepted
time: 16ms
memory: 137460kb

input:

10 100
abbacbbaabababcaabbaabbbabcaccabcabbabbbccaacabaabacccbacaaabbaaabbbaacaabbabcabbbcbacaaaaabaaaabaaacbbccbacacbbaaccacbabcbccacbaaacaaacccbabbaccacaaacaabbbccaaccbbabaabcbbcbcbb
bccabcacbacccbcababaccccaaabbccacaccccabbaabbcacabaccabacbbbaaaabaabccbcaacaaccbcacabcbbbcccccabccbaababccccabcabab...

output:

6384

result:

ok single line: '6384'

Test #3:

score: 20
Accepted
time: 12ms
memory: 140048kb

input:

50 50
aaaaabbabbbbbaababbaaaababaababababbbbaabaaaaaaaabaabbaaabaaaabaaabaabbbabbbababbbbabbbabbaaababbabaabbabbabbabbaaababaaaaabbbbbbaabaabaabbbbbabaaababb
abbabbbaabaaaaabababaabbabaabbbbab
bababbbbaabaabbaabbbbaabaabbbaabbbbaaabbabbbaaabbbbaaabaabbaaaaaabbbabaaaaabbaabaabaaabaaababbbbbaaabbbbbba...

output:

21362

result:

ok single line: '21362'

Test #4:

score: 20
Accepted
time: 7ms
memory: 137808kb

input:

50 50
aababcabbbcbaacaaaaccababbacbbaaabcbaaccabbababbcccaacaaaacacbcbaaabaacabbbbbbaccaaacbbacaacacbcccbbacabbcbcbacababcbbacaccaacacbcccbababaccabccccbabaccababccccbcbabcabcabccaaccababcbccacbbaaaacc
cbaccababccbaaacbccabbcccacacacaaaabaaababcacabaccbccacbcbaaaaabbacbbaccabba
ccbcbcacbcaccabbbccaa...

output:

13421

result:

ok single line: '13421'

Test #5:

score: 20
Accepted
time: 20ms
memory: 133336kb

input:

1000 1000
a
aabbab
bbbbababbbba
bb
baaaaa
ba
a
baa
a
bbaaaabaaaba
ba
a
a
a
bbababbbbbb
b
aaabb
bbbbbaabbabab
bbaaa
aaaa
aa
aaaaaababb
a
bbaba
baaa
aabbab
babaab
b
aab
bbbabb
aaaabbbbbaaaaaa
bbbbbbbaabab
bb
ab
aaa
aaababb
babaaaabab
aa
aaabaaababa
abbabaaaaabb
bbaa
abaabb
baa
abba
aaaa
abbbb
aab
b
aa...

output:

3159935

result:

ok single line: '3159935'

Subtask #2:

score: 30
Accepted

Test #6:

score: 30
Accepted
time: 188ms
memory: 209520kb

input:

1 10000
bcacacbcdadbcbccbdddaaddabcaccccbaabbcdcdaabdacccadacbbbabdaaacadacdccadcbdadadcaaabdbdbdcdcccbacbdbccacbdadcaaaacbabdadcacdcacadcadaabdaaabccccdcbcdbcadcaabbbaacaadaccdaccadbddcdbccbcddcaadcaadaadbbaadbcdbddbcbadbccbaddaacabcdccbadaddcadcbababccabcaadbcbdbcbbcdcbdbcdbdaaddbacddcabbbdccbddba...

output:

1160913325

result:

ok single line: '1160913325'

Test #7:

score: 30
Accepted
time: 40ms
memory: 171480kb

input:

1 1000
caaadbacdaaccbdcaccbdbddcdaccaaaaaccdccaadbadbcdccaacdadcccaddadcbacbbdcaadbbcdaddcadddccdbabccdadcaacabcabbbdbbbbdbbaaccddcdddcddddddbdbbadbddcbacdcdbdabcbbdbbaaadbdacaccdbaaabacbacabdaccaabaadddccbabacdbdddbdcadadcdccabaabbccbacddddcbcdbabaadaddbbdabadaccbdbaaabdadadaadabbdbadacacbdcbcbdccc...

output:

134272327

result:

ok single line: '134272327'

Test #8:

score: 30
Accepted
time: 178ms
memory: 211560kb

input:

1 10000
bbcbcaacaaaacacbacacbaacbacbaababaacacbacbacbccaacbbacbbccababcaaaccabaaccbbbaaaabbababaacbbccabcaccaaccbacccaacabcaacacaccababbbbbcbacaaabbcbaccacaaaaabbacbbcbbcabccaabcabcabaabbbabbababbabcacbcaccabacaababbacccabbcaaacacccbcbcccbbbbaababbabcaaabaccccbbbabccabacbcbabccccaccbcccccbcccccccbbc...

output:

1375114968

result:

ok single line: '1375114968'

Test #9:

score: 30
Accepted
time: 197ms
memory: 216708kb

input:

1 10000
cbaacaaaaccacaacbbcacabccbbabbababbababbacbababbcabcbacabbaaaacccacbbaaacacbacccbababaabccbbcabbbcaacbcbbbcbcbccccbabcacccccaabbbccacccacabccbcccbaaaccbbbacbaabbcbaabccccaabacabccaaccbbbaccbbcbcaabcaacbcbbcacbcbcabbacbababcbcabcbaabbbbbcccccbcacbabbcccccaacccbaccbaacbccbcababbabcbbccbbbbacab...

output:

1363955024

result:

ok single line: '1363955024'

Test #10:

score: 30
Accepted
time: 201ms
memory: 217552kb

input:

1 10000
aacbabccacabcccbccbbcacbbcbcacbbcacbbcbcacacbaaabbbbccaabcbbccccaccccbbcbbcacbbcbbcacbbcbcacacbaaabbbbccaabcbbccccaccccbbcbcbbccccaccccbbbccccaccccbbcbcbbccccaccccbbcbaaccbbbbaaaaabbbbccaabcbbccccaccccbbcbcbbccccaccccbbbccccaccccbbcbcbbccccaccccbbcbaaccbbbbaaabcacacbcbbcacbbcacacbaaabbbbccaa...

output:

1951994915

result:

ok single line: '1951994915'

Test #11:

score: 30
Accepted
time: 195ms
memory: 194524kb

input:

1 10000
bbaabbaaaabbbaaabbbaabaabaababbbbbabaabbababaaabbbbbbabbaaaabbbbbbaabababbbbabbaababbbbabaabaabaaabbabababbbbbaabababbaabbabbbababbbabaaaaabaaaaaaabaabaaabababbbabbabbaaaabababbbbabaabbaaababbaabbbbabbabbabbabbabaababaaaabbbaabbaababbabbbbaaaaaaaaaababbbbabbbabbaabbbbaaabaabbbbbaabbaababbaba...

output:

424739578

result:

ok single line: '424739578'

Subtask #3:

score: 20
Accepted

Test #12:

score: 20
Accepted
time: 657ms
memory: 148700kb

input:

100 1000
abbabbbbabbbbabaabaabbbbabaaabbbaabaabbabaabaabbaabbaabbababbabbababbbaabaabbaabbbabaabbbaaaabaaaabaababbaaaaaaaaabaabbaababbbbbbabbbabbbbababbbaaaaaabaaaabaaabbabbaaaaabbbabbabaabbabababbaaababbbaaaabbabbbabbabbabbbbbbabaabaabbbaabaaabbaabbabbbbaabbabaaabaabaabbbabbbbbabaababbbabababaabbba...

output:

1289287

result:

ok single line: '1289287'

Test #13:

score: 20
Accepted
time: 3659ms
memory: 159268kb

input:

1000 1000
babbbabaabaaaabbbbbababaaabbaabbbbbabbaababbbbaaabbbbaaabbbababbaaaabaaabbbbbbbbaabbabbbabbabbaaabaabbbbbbbababaabbbbabaaabbaaaaabbbaababbaaaaabbbbaaaabbbaabaaababaaabbabbbabbabbbabbaabbabbbabaaaaaabaabbabbababbbbbabaaabbbbababbbaabbbaaabbababaababbbbbaaabbbabbabaaaababbbaabaaaabbbabaaabbb...

output:

10431998

result:

ok single line: '10431998'

Test #14:

score: 20
Accepted
time: 2986ms
memory: 193520kb

input:

1000 10000
babbababbbaaaaaaaaababbabbbababbbbbbbbabaaaaaabbaababbbbbbbaaaabbaaaaabbbbbbbababbabaaaaabbaaabaaaaaaabbbabbabbaaabbbabbbbababababbabbababbabbbbaabbbbaababbaababbababbbaaabbbbaaabaaaabababbbbaabaababbabbbaabbbbbabbbaaabaabaabbaabaaaaabbabbbbaaabbabbbaababbbaabaaaabbbababbbbbbbabababbaabab...

output:

94347164

result:

ok single line: '94347164'

Test #15:

score: 20
Accepted
time: 373ms
memory: 231896kb

input:

10000 10000
bbbabbaababbbbbababbbbaabbabaaaababbabbbabababaaaaaababbbaabaabbababbbbbaa
b
aaabbbbbbabbaabbbbbbaabaaababbbabbabaaabababbababaaabbaabaabaabbbbabb
abbaabbaababbbabaabababaaaaaaaabaabbbb
bbbbbabbaabbaaaabaabaababbbababbbbbaaaabbabbbabbaabbbabaaababababaabbaaaabaabbaaabbbaaaaaababababbbabb...

output:

694099162

result:

ok single line: '694099162'

Test #16:

score: 20
Accepted
time: 626ms
memory: 138332kb

input:

100 100
ababababbbababbabbaabbbbbaabaaaaabbabaaaaabbbaabbbabababbbaaaaababbaabbbababaababbaabbaaababaabbaabbbbbbaaababbbaabaaaababbaababbbaaaabbbbaababaabaabaaabaaaabaababaaababbbbabbaabbbbababbaaabaaabaabaabaaaabaaaabbbbbabaaaabababaaababbbbbabbaaaabbbaaaaaaabababbaaababbbabbbbbaaaaaaaaaabbaabababa...

output:

138444

result:

ok single line: '138444'

Subtask #4:

score: 30
Accepted

Test #17:

score: 30
Accepted
time: 761ms
memory: 146884kb

input:

100 1000
bcbccabbbabacabaabbccbaaabbbbcbbbaaaacaaabbccbababbacbbabbbaccccccccbccabcaacbbccbbbbcaaacabcccaccbaacccbcbaaaccbcaaaaaaaabccaaaacccbabacbcababbacbbbcaabaaacbbcaccbaccccbbcbacabaacbcaccacbcbcbbcbccccbbcacccaabbabcaabbacabaaacccbbcbccbabbcbccbcbbbabbaabacbccbacababbacbababbbaacccacbabcabcaba...

output:

833103

result:

ok single line: '833103'

Test #18:

score: 30
Accepted
time: 4444ms
memory: 158900kb

input:

1000 1000
cacccababcaaccbaaabcbcabcbbccbcbcbbcacbbcabcaaccabcccaacabccaacaaaababbbbbcbbcbcbbbcacbaabccaccbaacaaacabbbcbcbaabbcccacaababbbabcaabbaabaabaabaabccaacccbabcbaacccbbbccaacbbacacacaacccbcbcbaccbccaaacabaacbcbaacbcbacaccbacbbcaccbabbbabbbaccccbbbcacccccaacbbaaaccacabaaacaccabacacbaaaaacbacab...

output:

6757759

result:

ok single line: '6757759'

Test #19:

score: 30
Accepted
time: 3358ms
memory: 198832kb

input:

1000 10000
cbacbccbcbbcccbbcccaacccbbbacbccaaaccabacaabacccbbbcbabcaacabbabbabcaaaabccbabcaaacaaabbbacccacabcacccaaababcacbaacaccbccbcaaaaaaaaccabbcaaaaabccacaacbccabacabcccaabbcaccbcabbccbabbbccaacbabcbacacabbaaaababbccbcbacbbbcaccaabacbcaabbaaacaacccaaabbbcabbbcccacacaccaacbccabccbcacbbbbbabcbabbc...

output:

61388196

result:

ok single line: '61388196'

Test #20:

score: 30
Accepted
time: 292ms
memory: 236392kb

input:

10000 10000
aababaaccbcbabaccbaccbcaaabbcbbbbacbcbababbccbccbbac
cbcccccacaccabcbbbaaabcaaccabccccccbabacbbabbaaacbabccbcaacacbbbabbbbcbbcbcbaaccaccbabccabbbbbabcabcbcccababbcabaccbbccbaaacaabacbcbcbbabbcbaabcaccaaaccbbcacaaabcabacabccbaabbacabccacbabbaababb
cacbcbabc
caacccaabaaccbbbabaababbbbcbcac...

output:

462062051

result:

ok single line: '462062051'

Test #21:

score: 30
Accepted
time: 697ms
memory: 138116kb

input:

100 100
abcababbbbbcaccccbcacacabbccccbbccacbbabacabbbbacacaacaaabbcaabcaaaaabaaccbaaacbbcbbcabbaaababacabcccaabbcbbbbaaaacaabbbacbcabacbbccbbacacbaaabacabbcbbcaabaccababccaaababbcbcacbabababcbcccccccababcbbbaaabbacbaabbaababcabaacbccacbbaccbbbbcbbabbccaacbaccaaabaccbaacaaaaabbcaccbbbaaccaaccccbbbaa...

output:

90325

result:

ok single line: '90325'

Test #22:

score: 30
Accepted
time: 1381ms
memory: 115132kb

input:

430 800
aaaccaccaacbcccbccbccaaaaaabccabbcbccabcacbcaabcbacbccbbacccaccabccacbbcccccbacaacbabbbaaaaacbbbcabacbccaabcabcbacccacbabaacacbcaacbabbabbbbbaacacabbaccaabcccbacbbabccccccbabbaaaaababcababaabcaabbcbbaaccaccabbaabcabccbbcbacacaaabcbaaccccbcbacbccacaaacbbaabaabccabaacbccccbbbbcabccbbcbbcbccaba...

output:

157989035

result:

ok single line: '157989035'

Test #23:

score: 30
Accepted
time: 406ms
memory: 137772kb

input:

400 400
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

40039936

result:

ok single line: '40039936'

Test #24:

score: 30
Accepted
time: 1653ms
memory: 140436kb

input:

400 400
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbabaaaaabbbbaabbb...

output:

108484268

result:

ok single line: '108484268'