zoukankan      html  css  js  c++  java
  • 【bzoj3555】[Ctsc2014]企鹅QQ

    题目描述:

    PenguinQQ是中国最大、最具影响力的SNS(Social Networking Services)网站,以实名制为基础,为用户提供日志、群、即时通讯、相册、集市等丰富强大的互联网功能体验,满足用户对社交、资讯、娱乐、交易等多方面的需求。
    小Q是PenguinQQ网站的管理员,他最近在进行一项有趣的研究——哪些账户是同一个人注册的。经过长时间的分析,小Q发现同一个人注册的账户名称总是很相似的,例如Penguin1,Penguin2,Penguin3……于是小Q决定先对这种相似的情形进行统计。
    小Q定义,若两个账户名称是相似的,当且仅当这两个字符串等长且恰好只有一位不同。例如“Penguin1”和“Penguin2”是相似的,但“Penguin1”和“2Penguin”不是相似的。而小Q想知道,在给定的 个账户名称中,有多少对是相似的。
    为了简化你的工作,小Q给你的 个字符串长度均等于 ,且只包含大小写字母、数字、下划线以及‘@’共64种字符,而且不存在两个相同的账户名称。

    输入:
    第一行包含三个正整数 , , 。其中 表示账户名称数量, 表示账户名称长度, 用来表示字符集规模大小,它的值只可能为2或64。
    若 等于2,账户名称中只包含字符‘0’和‘1’共2种字符;
    若 等于64,账户名称中可能包含大小写字母、数字、下划线以及‘@’共64种字符。
    随后 行,每行一个长度为 的字符串,用来描述一个账户名称。数据保证 个字符串是两两不同的。

    输出:

    仅一行一个正整数,表示共有多少对相似的账户名称。

    样例输入:

    4 3 64
    Fax
    fax
    max
    mac

    样例输出:

    4

    题解:

    把枚举每个字符串抠掉哪一位,然后插进哈希表里边,如果出现过就统计答案。

    代码:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    
    #ifdef WIN32
    	#define LL "%I64d"
    #else
    	#define LL "%lld"
    #endif
    
    #ifdef CT
    	#define debug(...) printf(__VA_ARGS__)
    	#define setfile() 
    #else
    	#define debug(...)
    	#define filename ""
    	#define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);
    #endif
    
    #define R register
    #define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
    #define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
    #define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
    #define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
    #define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
    char B[1 << 15], *S = B, *T = B;
    inline int FastIn()
    {
    	R char ch; R int cnt = 0; R bool minus = 0;
    	while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ;
    	ch == '-' ? minus = 1 : cnt = ch - '0';
    	while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';
    	return minus ? -cnt : cnt;
    }
    #define hashsize 19991023
    struct HashTable
    {
    	unsigned long long _key1, _key2;
    	int val;
    	HashTable *next;
    }ha[hashsize], *last[hashsize], *tot = ha;
    char str[210];
    long long ans, p[210];
    inline HashTable *Hash(R unsigned long long key1, R unsigned long long key2)
    {
    	for (R HashTable *pos = last[key1 % hashsize]; pos; pos = pos -> next) if (pos -> _key1 == key1 && pos -> _key2 == key2) return pos;
    	*++tot = (HashTable){key1, key2, 0, last[key1 % hashsize]};
    	last[key1 % hashsize] = tot;
    	return tot;
    }
    int main()
    {
    //	setfile();
    	R int n, l, s;
    	scanf("%d%d%d
    ", &n, &l, &s);
    	p[0] = 1;
    	for (R int i = 1; i <= l; ++i) p[i] = p[i - 1] * 233;
    	for (R int i = 1; i <= n; ++i)
    	{
    		gets(str + 1);
    		R unsigned long long h1 = 0, h2 = 0;
    		for (R int j = 1; j <= l; ++j)
    			h1 += str[j] * p[j], h2 ^= str[j] * p[j];
    		for (R int j = 1; j <= l; ++j)
    		{
    			R unsigned long long hh1 = (h1 - str[j] * p[j]) % hashsize, hh2 = h2 ^ str[j] * p[j];
    			HashTable *hahaha = Hash(hh1, hh2);
    			if (hahaha -> val > 0) ans += hahaha -> val;
    			hahaha -> val ++;
    		}
    	}
    	printf("%lld
    ",ans );
    	return 0;
    }
    



  • 相关阅读:
    在IE和Firfox获取keycode
    using global variable in android extends application
    using Broadcast Receivers to listen outgoing call in android note
    help me!virtual keyboard issue
    using iscroll.js and iscroll jquery plugin in android webview to scroll div and ajax load data.
    javascript:jquery.history.js使用方法
    【CSS核心概念】弹性盒子布局
    【Canvas学习笔记】基础篇(二)
    【JS核心概念】数据类型以及判断方法
    【问题记录】ElementUI上传组件使用beforeupload钩子校验失败时的问题处理
  • 原文地址:https://www.cnblogs.com/cocottt/p/6765013.html
Copyright © 2011-2022 走看看