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. LeetCode5.最长回文子串 JavaScript

    给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad" 输出: "bab" 注意: &qu ...

  2. java HtmlEmail发送邮件工具类

    package com.sh.xrsite.common.utils; import java.io.File; import java.util.HashMap; import java.util. ...

  3. LeetCode 简单 - 路径总和(112)

    给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = 22 ...

  4. boost::shared_ptr文档翻译

    shared_ptr: 共享所有权 原文链接 描述 模版类 shared_ptr 存储动态构造对象的指针,通常是由C++ new语句完成的.这个对象指针在最后一个持有指针所有权的shared_ptr被 ...

  5. 魔板 Magic Squares(广搜,状态转化)

    题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 我们知道魔板的每一个方格都有一种颜色.这8种颜 ...

  6. 爬虫——BeautifulSoup4解析器

    BeautifulSoup用来解析HTML比较简单,API非常人性化,支持CSS选择器.Python标准库中的HTML解析器,也支持lxml的XML解析器. 其相较与正则而言,使用更加简单. 示例: ...

  7. android 自定义图片圆形进度条

    感觉话一个圆形进度条挺简单的 ,但是却偏偏给了几张图片让你话,说实话我没接触过,感觉好难,还好百度有大把的资源,一番努力下终于画出来了. 代码如下. package com.etong.cpms.wi ...

  8. webpack4提升180%编译速度

    前言 对于现在的前端项目而言,编译发布几乎是必需操作,有的编译只需要几秒钟,快如闪电,有的却需要10分钟,甚至更多,慢如蜗牛.特别是线上热修复时,分秒必争,响应速度直接影响了用户体验,用户不会有耐心等 ...

  9. C#中在WebClient中使用post发送数据实现方法

    很多时候,我们需要使用C#中的WebClient 来收发数据,WebClient 类提供向 URI 标识的任何本地.Intranet 或 Internet 资源发送数据以及从这些资源接收数据的公共方法 ...

  10. ODBC error in PHP: “No tuples available at this result index”

    ODBC error in PHP: “No tuples available at this result index” 在执行存储过程的时候发生如题的错误,在stackoverflow上找到了相同 ...