BZOJ 3555: [Ctsc2014]企鹅QQ hash
3555: [Ctsc2014]企鹅QQ
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://www.lydsy.com/JudgeOnline/problem.php?id=3555
Description
PenguinQQ是中国最大、最具影响力的SNS(Social Networking Services)网站,以实名制为基础,为用户提供日志、群、即时通讯、相册、集市等丰富强大的互联网功能体验,满足用户对社交、资讯、娱乐、交易等多方面的需求。
小Q是PenguinQQ网站的管理员,他最近在进行一项有趣的研究——哪些账户是同一个人注册的。经过长时间的分析,小Q发现同一个人注册的账户名称总是很相似的,例如Penguin1,Penguin2,Penguin3……于是小Q决定先对这种相似的情形进行统计。
小Q定义,若两个账户名称是相似的,当且仅当这两个字符串等长且恰好只有一位不同。例如“Penguin1”和“Penguin2”是相似的,但“Penguin1”和“2Penguin”不是相似的。而小Q想知道,在给定的 个账户名称中,有多少对是相似的。
为了简化你的工作,小Q给你的 个字符串长度均等于 ,且只包含大小写字母、数字、下划线以及‘@’共64种字符,而且不存在两个相同的账户名称。
小Q是PenguinQQ网站的管理员,他最近在进行一项有趣的研究——哪些账户是同一个人注册的。经过长时间的分析,小Q发现同一个人注册的账户名称总是很相似的,例如Penguin1,Penguin2,Penguin3……于是小Q决定先对这种相似的情形进行统计。
小Q定义,若两个账户名称是相似的,当且仅当这两个字符串等长且恰好只有一位不同。例如“Penguin1”和“Penguin2”是相似的,但“Penguin1”和“2Penguin”不是相似的。而小Q想知道,在给定的 个账户名称中,有多少对是相似的。
为了简化你的工作,小Q给你的 个字符串长度均等于 ,且只包含大小写字母、数字、下划线以及‘@’共64种字符,而且不存在两个相同的账户名称。
Under two situations the player could score one point.
⋅1. If you touch a buoy before your opponent, you will get one point. For example if your opponent touch the buoy #2 before you after start, he will score one point. So when you touch the buoy #2, you won't get any point. Meanwhile, you cannot touch buoy #3 or any other buoys before touching the buoy #2.
⋅2. Ignoring the buoys and relying on dogfighting to get point. If you and your opponent meet in the same position, you can try to fight with your opponent to score one point. For the proposal of game balance, two players are not allowed to fight before buoy #2 is touched by anybody.
There are three types of players.
Speeder: As a player specializing in high speed movement, he/she tries to avoid dogfighting while attempting to gain points by touching buoys.
Fighter: As a player specializing in dogfighting, he/she always tries to fight with the opponent to score points. Since a fighter is slower than a speeder, it's difficult for him/her to score points by touching buoys when the opponent is a speeder.
All-Rounder: A balanced player between Fighter and Speeder.
There will be a training match between Asuka (All-Rounder) and Shion (Speeder).
Since the match is only a training match, the rules are simplified: the game will end after the buoy #1 is touched by anybody. Shion is a speed lover, and his strategy is very simple: touch buoy #2,#3,#4,#1 along the shortest path.
Asuka is good at dogfighting, so she will always score one point by dogfighting with Shion, and the opponent will be stunned for T seconds after dogfighting. Since Asuka is slower than Shion, she decides to fight with Shion for only one time during the match. It is also assumed that if Asuka and Shion touch the buoy in the same time, the point will be given to Asuka and Asuka could also fight with Shion at the buoy. We assume that in such scenario, the dogfighting must happen after the buoy is touched by Asuka or Shion.
The speed of Asuka is V
Input
第一行包含三个正整数 , , 。其中 表示账户名称数量, 表示账户名称长度, 用来表示字符集规模大小,它的值只可能为2或64。
若 等于2,账户名称中只包含字符‘0’和‘1’共2种字符;
若 等于64,账户名称中可能包含大小写字母、数字、下划线以及‘@’共64种字符。
随后 行,每行一个长度为 的字符串,用来描述一个账户名称。数据保证 个字符串是两两不同的。
Output
仅一行一个正整数,表示共有多少对相似的账户名称。
Sample Input
4 3 64
Fax
fax
max
mac
Sample Output
4
HINT
题意
题解:
直接暴力hash就好了,枚举哪一位不一样就行了……
不要想多了
代码
#include<iostream> #include<stdio.h> #include<algorithm> #include<vector> using namespace std; long long N=1000000007; long long p=149; long long powp[305*4]; int num,n,k; void get_hash(long long h[],char str[]) { h[0]=(long long)str[0]; for(int i=1;i<n;i++) h[i]=(h[i-1]*p+(long long)str[i]); } char s[30005][205]; long long h[30005][205]; int main() { scanf("%d%d%d",&num,&n,&k); vector<long long> Q; powp[0]=1LL; for(int i=1;i<=n;i++) powp[i]=powp[i-1]*p; for(int i=0;i<num;i++) { scanf("%s",s[i]); get_hash(h[i],s[i]); } long long ans = 0; for(int i=0;i<n;i++) { Q.clear(); for(int j=0;j<num;j++) { long long p = h[j][n-1]-h[j][i]*powp[n-i-1]+h[j][i-1]*powp[n-i]; Q.push_back(p); } sort(Q.begin(),Q.end()); int tmp = 0; for(int j=0;j<num;j++) { if(j==0||Q[j]!=Q[j-1]) tmp=-1; tmp++; ans += tmp; } //cout<<ans<<endl; } printf("%lld\n",ans); }