列表

详情


NC221779. BirthdayCake

描述

Moca's birthday is coming up, and her friend Ran is going to the Yamabuki bakery to order a birthday cake for her.

Yamabuki bakery provides kinds of cake. Since Ran knows that Moca is the Yamabuki Bakery's -st fan and can eat a lot of food, she intends to order two cakes and put them together into a big cake. The cake made by Yamabuki bakery can be formed by a string consisting of lowercase Latin letters, which describes the toppings on the cake, such as strawberries and chocolate. Putting two cakes together is the concatenation of two corresponding strings. For example, the result of putting cake and cake together is a big cake .

To make it easier to share the cake, Ran will choose two cakes and put them together into a big cake which can be divided in half into two identical pieces. For example, cake  will be divided in half into two cakes , but cake  will be divided in half into two different cakes  and . Notice that Ran can not reverse the cake, which means that cake  will be divided into two different cakes  and .

Can you help Ran figure out how many different pairs of cakes meet the above condition?

输入描述

The first line contains one integer   -- the number of cakes.

The next lines describe all the cakes, where the -th line contains one string s_i  consisting of lowercase Latin letters.

It is guaranteed that the sum of lengths of cakes does not exceed .

输出描述

Print one integer -- the number of pairs of cakes meet the above condition.

示例1

输入:

3
ab
ab
cabc

输出:

3

示例2

输入:

3
abc
a
cabc

输出:

0

示例3

输入:

4
hhh
hhh
hhh
hhh

输出:

6

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 109ms, 内存消耗: 33992K, 提交时间: 2021-05-28 00:06:02

#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include<bits/stdc++.h>
#define int long long
#define LL __int128
using namespace std;
const int N=4e5+10;
const int base=131,mod=1e18+3;
LL h[N],pw[N];
int n,id[N],res; string str[N]; unordered_map<int,int> mp;
LL get(int l,int r){return (h[r]-h[l-1]*pw[r-l+1]%mod+mod)%mod;}
signed main(){
	cin>>n; pw[0]=1;
	for(int i=1;i<=n;i++) cin>>str[i],id[i]=i;
	for(int i=1;i<N;i++) pw[i]=pw[i-1]*base%mod;
	sort(id+1,id+1+n,[](int a,int b){return str[a].size()<str[b].size();});
	for(int i=1,m;i<=n;i++){
		h[0]=str[id[i]][0],m=str[id[i]].size()-1;
		for(int j=1;j<=m;j++) h[j]=(h[j-1]*base+str[id[i]][j])%mod;
		for(int j=(m+1)/2;j<m;j++)	if(get(0,m-j-1)==get(j+1,m)) res+=mp[get(m-j,j)];
		res+=mp[h[m]]++;
	}
	cout<<res;
	return 0;
}

上一题