Description

Davy Jones: You've been captain of the Black Pearl for 13 years. That was our agreement. Jack: Technically I was only captain for two years, then I was mutinied upon. Davy Jones: Then you were a poor captain, but a captain nonetheless. Have you not introduced yourself as Captain Jack Sparrow?
According to the Pirate Code, each of the pirates of the Caribbean at the beginning of their professional career (hereditary pirates –– at birth) is assigned by a unique identifier. Pirate's identifier is a string of four hexadecimal digits. However, it is not a usual row of numbers, it is said that personal qualities and life path of its owner are encoded in it by a mysterious way. But no one still could guess this mystical connection.
Once Captain Jack Sparrow, while sitting in captain’s cabin, decided to try to find the way to derive some data about a pirate using the identifier. Memories about how he lost the Black Pearl last time gave him the idea that more similar identifiers of two pirates are, bigger chances for these pirates to unite against the Captain, and, as a result, to make a mutiny. The Captain Jack Sparrow, of course, doesn’t want to have the mutiny on his ship, but he chose the new team this time and it is going to be a long voyage. Now Jack needs to estimate the opportunities of raising the mutiny on his ship, based on the conclusions. For this aim he first wants to know for each pair of pirates a number of positions in their identifiers in which they are different.

Input

The first line contains an integer n –– the number of pirates aboard the Black Pearl (2 ≤ n ≤ 65536). Each of the following n lines contains four-digit identifier of the respective pirate. Only decimal digits and lowercase Latin letters from “a” to “f” inclusive are used in writing identifiers. Identifiers of all pirates are different.

Output

Output four space separated integers –– the amount of pairs of pirates, which have different identifiers exactly in one, two, three and four positions respectively.

题目大意:给n个字符串,每个字符串有4个字符(其实是4位16进制数),输出这些字符串中有1位不同的字符串个数、有2位不同的字符串个数、有3位不同的字符串个数、有4位不同的字符串个数.

思路:用一个数组sum[i][j]存下在状态 i 下 j 的出现次数,如sum[3][244]就代表XXF4的出现次数:其中3是二进制的0011,代表是计算最后两位的出现次数,前面两位为任意字符;F4(10进制为244)则为后两位为F4的字符串的出现次数;XX为任意字符。

那么,扫一遍,统计一下,就能知道:一个字符相同的对数、两个字符相同的对数、三个字符相同的对数。注意,这样算的时候,如aaaa和aaab,这个一个字符相同的对数是算了3遍的,所以要减回去。

至于怎么减回去大概可以用容斥原理,我写的那些其实是我自己YY的,我不会容斥o(╯□╰)o

代码(62MS):

 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL; LL sum[][ << ];
char s[];
int n, maxl; inline int trans(char c) {
if(isdigit(c)) return c - '';
return c - 'a' + ;
} void init() {
for(int p = ; p < n; ++p) {
scanf("%s", &s[]);
for(int i = ; i <= ; ++i) {
int t = ;
if(i&) t += trans(s[]) * (<<);
if(i&) t += trans(s[]) * (<<);
if(i&) t += trans(s[]) * (<<);
if(i&) t += trans(s[]);
++sum[i][t];
maxl = max(maxl, t);
}
}
} LL ans[], tmp[]; inline int bit_count(int x) {
int ret = ;
while(x > ) ret += (x & ), x >>= ;
return ret;
} void solve() {
for(int i = ; i <= ; ++i) {
int x = bit_count(i);
for(int j = ; j <= maxl; ++j) tmp[x] += sum[i][j] * (sum[i][j] - ) / ;
}
ans[] = tmp[];//1位不同=3位相同
ans[] = tmp[] - * tmp[];
ans[] = tmp[] - * tmp[] + * tmp[];
ans[] = LL(n - ) * n / - ans[] - ans[] - ans[];
cout<<ans[]<<' '<<ans[]<<' '<<ans[]<<' '<<ans[]<<endl;
} int main() {
scanf("%d", &n);
init();
solve();
}

URAL 1932 The Secret of Identifier(容斥)的更多相关文章

  1. ural 1932 The Secret of Identifier 容斥

    主题链接:点击打开链接 stl+容斥 #include <cstdio> #include <cstring> #include <algorithm> #incl ...

  2. URAL 1932 The Secret of Identifier 题解

    http://acm.timus.ru/problem.aspx?space=1&num=1932 B - The Secret of Identifier Time Limit:1000MS ...

  3. ural 1932 The Secret of Identifier (容斥原理)

    标题效果: 计算到n字符串. 精确到只是有一个不同的字符,两个不同的字符.三个不同的字符,四对不同的字符. IDEAS: 枚举状态. dp[i] [j] ...当前串取出 i 状态下的全部字符转化成十 ...

  4. Tmutarakan Exams URAL - 1091(莫比乌斯函数 || 容斥)

    题意: 求1 - s 中 找出k个数 使它们的gcd  > 1 求这样的k个数的对数 解析: 从每个素数的倍数中取k个数  求方案数 然后素数组合,容斥一下重的 奇加偶减 莫比乌斯函数的直接套模 ...

  5. HDU 5514 Frogs 容斥定理

    Frogs Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5514 De ...

  6. hdu 5514 Frogs(容斥)

    Frogs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  7. hdu 5514 Frogs 容斥思想+gcd 银牌题

    Frogs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  8. Lucky HDU - 5213 (莫队,容斥)

    WLD is always very lucky.His secret is a lucky number . is a fixed odd number. Now he meets a strang ...

  9. POJ1091跳蚤(容斥 + 唯一分解 + 快速幂)

      题意:规定每次跳的单位 a1, a2, a3 …… , an, M,次数可以为b1, b2, b3 …… bn, bn + 1, 正好表示往左,负号表示往右, 求能否调到左边一位,即 a1* b1 ...

随机推荐

  1. Oracle客户端与Toad、plsql developer安装

    (一)oracle client与oracle instant client比较 当我们要使用Toad.plsql developer等工具连接数据库时,首先需要在自己的电脑上安装oracle cli ...

  2. selenium之Xpath定位

    1. 绝对定位: driver.find_element_by_xpath("/html/body/div[x]/form/input") x 代表第x个 div标签,注意,索引从 ...

  3. Angularjs基础(六)

    AngularJS HTML DOM AngularJS为HTML DOM 元素的属性提供了绑定应用数据的指令. ng-disabled指令 ng-disabled指令直接绑定应用数据到HTML的di ...

  4. Sass 基础(一)

    css 是一些非常简单得语句的组合,既然简单的语句,就不可避免的有很多重复的,冗余的东西,而且没有传统编程语言变量,控制语句等高级特性,所以造成了css 编写低效,往往需要查找替换,大量复制来修改或者 ...

  5. python list统计

    from random import randint data = [randint(0, 20) for _ in xrange(30)] print data # [20, 4, 4, 20, 1 ...

  6. mysql帐号不允许从远程登陆

    默认情况下,mysql帐号不允许从远程登陆,只能在localhost登录.本文提供了二种方法设置mysql可以通过远程主机进行连接. 一.改表法 在localhost登入mysql后,更改 “mysq ...

  7. Mysql基础2-数据定义语言DDL

    主要: 数据库操作语句 数据表操作语句 视图定义语句 数据库表设计原则 DDL: Data Definition Language 数据定义语言 数据库操作语句 创建库 创建数据库: create d ...

  8. css文本截字,超出文本省略号显示

    一.单行文本截字 p { text-overflow: ellipsis;/*显示省略号代替裁剪的文本*/ white-space: nowrap;/*空白处理方式 不换行*/ overflow: h ...

  9. Python学习 :文件操作

    文件基本操作流程: 一. 创建文件对象 二. 调用文件方法进行操作 三. 关闭文件(注意:只有在关闭文件后,才会写入数据) fh = open('李白诗句','w',encoding='utf-8') ...

  10. admin添加用户时报错:(1452, 'Cannot add or update a child row: a foreign key constraint fails (`mxonline`.`django_admin_l

    在stackoverflow找到答案: DATABASES = { 'default': { ... 'OPTIONS': { "init_command": "SET ...