问题 G: Green Bin

时间限制: 1 Sec  内存限制: 128 MB
[提交] [状态]

题目描述

We will call a string obtained by arranging the characters contained in a string a in some order, an anagram of a.
For example, greenbin is an anagram of beginner. As seen here, when the same character occurs multiple times, that character must be used that number of times.
Given are N strings s1,s2,…,sN. Each of these strings has a length of 10 and consists of lowercase English characters. Additionally, all of these strings are distinct. Find the number of pairs of integers i,j (1≤i<j≤N) such that si is an anagram of sj.

Constraints
·2≤N≤105
·si is a string of length 10.
·Each character in si is a lowercase English letter.
·s1,s2,…,sN are all distinct.

输入

Input is given from Standard Input in the following format:

N
s1
s2
:
sN

输出

Print the number of pairs of integers i,j (1≤i<j≤N) such that si is an anagram of sj.

样例输入
Copy

【样例1】
3
acornistnt
peanutbomb
constraint
【样例2】
2
oneplustwo
ninemodsix
【样例3】
5
abaaaaaaaa
oneplustwo
aaaaaaaaba
twoplusone
aaaabaaaaa

样例输出 Copy

【样例1】
1
【样例2】
0
【样例3】
4

提示

样例1解释
s1= acornistnt is an anagram of s3= constraint. There are no other pairs i,j such that si is an anagram of sj, so the answer is 1.
样例2解释
If there is no pair i,j such that si is an anagram of sj, print 0.
样例3解释
Note that the answer may not fit into a 32-bit integer type, though we cannot put such a case here.
解析:

题意概括:

找出有这些单词所含的字母种类和数目都相等的对数。注意单词之间可以随意组合,因此要用到排列的知识。

解题思路:

首先将字符串内的字符进行从小到大排列,这样就很容易比较两个字符串是否满足题意要求。使用map容器记录相同单词数,最后使用排列知识算出总个数。给你一些字符串,判断其中有多少对字符串调换字符位置后会得到一样的字符串(组成字母及数目一样)
由组合数知识知n个相同组成的字符串可以构成C2n对,即n*(n-1)/2

如果是这样定义

mp[99] = 1000000;

it->first就是下标,即99
it->second是值,即1000000

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<map>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
const int maxn=1e6;
string a;
map<string,ll>mp;
int n;
void inint(){
cin>>n;
for(int i=;i<n;i++){
cin>>a;
sort(a.begin(),a.end());
mp[a]++;
}
}
int main(){
inint();
ll sum=;
for(auto i=mp.begin();i!=mp.end();i++){
ll s=i->second;
sum+=(s*(s-))/;
}
printf("%lld",sum);
}

mp的解析:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<map>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
const int maxn=1e6;
string a;
map<string,ll>mp;
int main(){
cin>>a;
mp[a]=;
auto i=mp.begin();
cout<<i->first<<endl;//输入的字符串
cout<<i->second;//下标
}

输出:

mp的更多相关文章

  1. [公告]Senparc.Weixin.MP v14.2.1 升级说明

    在Senparc.Weixin.MP v14.2.1中,所有Senparc.Weixin.MP下的Container,命名空间已经从 Senparc.Weixin.MP.CommonAPIs 改为了  ...

  2. Senparc.Weixin.MP SDK 微信公众平台开发教程(十八):Web代理功能

    在Senparc.Weixin.dll v4.5.7版本开始,我们提供了Web代理功能,以方便在受限制的局域网内的应用可以顺利调用接口. 有关的修改都在Senparc.Weixin/Utilities ...

  3. Senparc.Weixin.MP SDK 微信公众平台开发教程(十七):个性化菜单接口说明

    前不久微信上线了个性化菜单接口,Senparc.Weixin SDK也已经同步更新. 本次更新升级Senparc.Weixin.MP版本到v13.5.2,依赖Senparc.Weixin版本4.5.4 ...

  4. 微信小程序 不在以下合法域名列表中,请参考文档:https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-request.html

    微信小程序  不在以下合法域名列表中,请参考文档:https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-request.html 友情提示: 大家 ...

  5. 微信公众账号 Senparc.Weixin.MP SDK 开发教程 索引

    Senparc.Weixin.MP SDK从一开始就坚持开源的状态,这个过程中得到了许多朋友的认可和支持. 目前SDK已经达到比较稳定的版本,这个过程中我觉得有必要整理一些思路和经验,和大家一起分享. ...

  6. Senparc.Weixin.MP SDK 微信公众平台开发教程(三):微信公众平台开发验证

    要对接微信公众平台的"开发模式",即对接到自己的网站程序,必须在注册成功之后(见Senparc.Weixin.MP SDK 微信公众平台开发教程(一):微信公众平台注册),等待官方 ...

  7. Senparc.Weixin.MP SDK 微信公众平台开发教程(四):Hello World

    =============  以下写于2013-07-20 ============= 这一篇文章其实可以写在很前面,不过我还是希望开发者们尽多地了解清楚原理之后再下手. 通过上一篇Senparc.W ...

  8. Senparc.Weixin.MP SDK 微信公众平台开发教程(五):使用Senparc.Weixin.MP SDK

    Senparc.Weixin.MP SDK已经涵盖了微信6.x的所有公共API. 整个项目的源代码以及已经编译好的程序集可以在这个项目中获取到:https://github.com/JeffreySu ...

  9. Senparc.Weixin.MP SDK 微信公众平台开发教程(六):了解MessageHandler

    上一篇<Senparc.Weixin.MP SDK 微信公众平台开发教程(五):使用Senparc.Weixin.MP SDK>我们讲述了如何使用Senparc.Weixin.MP SDK ...

  10. Senparc.Weixin.MP SDK 微信公众平台开发教程(七):解决用户上下文(Session)问题

    从这篇文章中我们已经了解了微信公众平台消息传递的方式,这种方式有一个先天的缺陷:不同用户的请求都来自同一个微信服务器,这使得常规的Session无法使用(始终面对同一个请求对象,况且还有对方服务器Co ...

随机推荐

  1. Codeforces Round #592 (Div. 2) E

    给你一个数组,你最多可以进行k次操作,每次操作可以使一个数+1或者-1,问操作之后数组的极差最小可能是多少 利用map来模拟移动,可以观察到每次应该选择数量少的一组数让他们进行移动是最优的 int m ...

  2. php弱语言特性-计算科学计数法

    php处理字符串时存在一个缺陷问题,如果字符串为“1e1”,本该是一个正常的字符串,但是php会将它认为是科学计数法里面的e: 也就是按照数学的科学计数法来说:1e1=10^1=10,因此php会把这 ...

  3. 关于Windows系统下端口被占用的问题和task命令

    一.如何解决端口被占用的问题? 此时端口4444被进程占用,只要找到端口4444的进程,并且将进程kill掉即可. 开始--运行--cmd 进入命令提示符 输入netstat -aon 即可看到所有连 ...

  4. C#索引器学习笔记

    本笔记摘抄自:https://www.cnblogs.com/ArmyShen/archive/2012/08/27/2659405.html,记录一下学习过程以备后续查用. 索引器允许类或者结构的实 ...

  5. 【并发那些事】线程有序化神器CompletionService

    前言 话说有一天,产品经理突然找到正在摸鱼的你. 产品:『我们要加一个聚合搜索功能,当用户在我们网站查询一件商品时,我们分别从 A.B.C 三个网站上查询这个信息,然后再把得到的结果返回给用户』 你: ...

  6. gulp常用插件之browser-sync使用

    更多gulp常用插件使用请访问:gulp常用插件汇总 browser-sync这是一个可以在多端(pc.移动.平板)实时监测文件修改,自动刷新浏览器的工具.其实这并不是转给gulp使用的,在其它构建工 ...

  7. 拦截器 Filter : js、css、jpg、png等静态资源不被拦截解决方案

    方案一: web.xml配置文件拦截范围缩小 ,没有必要 /*的配置拦截项目下所有资源. <filter> <filter-name>Login</filter-name ...

  8. win10文件夹 无法显示当前所有者 管理员都不行

    1.在win10系统桌面上,任务栏,右键,单击任务管理器. 2.单击性能. 3.单击打开资源监视器. 4.在单击CPU标签,然后再“关联的句柄”右侧的搜索框中输入要删除的文件夹名.例:tre文件夹名. ...

  9. BandwagonHost VPS CentOS7系统启用EPEL源

    BandwagonHost默认安装好了EPEL源,只是默认没有启用,所以很多软件都无法安装.我们需要手动启用一下,才能安装里面的软件. # vim /etc/yum.repos.d/epel.repo ...

  10. ArcGis Server manager 忘记用户名和密码

    ArcGIS 10.1及以后版本重置Server Manager账户密码:(1)找到arcgis server的安装目录,目录指向\ArcGIS\Server\tools\passwordreset文 ...