有的答案AC了,有的tle了,还有的wr了,有大佬能帮忙看看怎么优化吗
查看原帖
有的答案AC了,有的tle了,还有的wr了,有大佬能帮忙看看怎么优化吗
1695911
yxc0909楼主2025/7/27 11:52
#include<iostream>
using namespace std;
#include<cstring>
#include<utility>
#include<cstdio>
typedef unsigned long long ull;
int n;
char str1[15],str2[4];
ull h[4],p[4];
const int P=131;
pair <int,int> pairr[200010];
int j=1;
int cnt=0;
ull get(int l,int r)
{
	return h[r]-h[l-1]*p[r-l+1];
}
int main()
{
	scanf("%d",&n);
	while(n--)
	{
		scanf("%s",str1+1);
		scanf("%s",str2+1);
		for(int i=1;i<=2;i++)
		{
			p[i]=p[i-1]*P;
			h[i]=h[i-1]*P+str1[i];
		}
		int x1=get(1,2);
		memset(h,0,sizeof(h));
		for(int i=1;i<=2;i++)
		{
			h[i]=h[i-1]*P+str2[i];
		}
		int x2=get(1,2);
		memset(h,0,sizeof(h));
		pairr[j++]={x1,x2};
	}
	for(int i=1;i<j;i++)
	{
		for(int k=i+1;k<j;k++)
		{
			if(pairr[i].first ==pairr[k].second && pairr[k].first ==pairr[i].second )
			{
				cnt++;
			}
		}
	}
	printf("%d\n",cnt);
	return 0;
}
2025/7/27 11:52
加载中...