问题 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. ASP.NET Identity系列教程-3【运用ASP.NET Identity】

    https://www.cnblogs.com/r01cn/p/5180892.html 14 运用ASP.NET Identity In this chapter, I show you how t ...

  2. javaScript中的异步编程模式

    1.事件模型 let button = document.getElementById("my-btn"); button.onclick = function(event) { ...

  3. ffmpeg rtp rtmp udp 推流命令

    推组播 组播地址指的范围是224.0.0.0—239.255.255.255 ffmpeg -re -i chunwan.h264 -vcodec mpeg2video -f mpeg2video u ...

  4. 移动端 h5

    取消点击时候的透明区域 -webkit-tap-highlight-color: rgba(0,0,0,0); iphonex 适配 <meta name="viewport" ...

  5. CSS-使用CSS样式的方式

    1.HTML<!DOCTYPE>声明标签 (1)定义和用法 <!DOCTYPE>声明必须是HTML文档的第一行,位于<html>标签之前. <!DOCTYPE ...

  6. MySQL的选则字段+联表+判断+排序(order by)

    MySQL的选则字段+联表+判断+排序(order by) 两个表:1.成绩单 2.查询名单 目标: 1.选中全部字段,用于输出. 2.成绩单中有很多人的成绩,第一步是希望通过联表,只查查询名单上的人 ...

  7. 搜索字母a或A

    Amy觉得英语课实在是无聊至极,他不喜欢听老师讲课. 但是闲着也是闲着,不如做点什么吧?于是他开始数英语书里的字母a和A共出现了多少次. 费了九牛二虎之力终于数完了. 作为一名软件工程专业大学生,他觉 ...

  8. 《深入理解java虚拟机》读书笔记十——第十一章

    第十一章  晚期(运行期)优化 1.HotSpot虚拟机内的即时编译 解释器与编译器: 许多Java虚拟机的执行引擎在执行Java代码的时候都有解释执行(通过解释器执行)和编译执行(通过即时编译器产生 ...

  9. Xamarin.Android 开发,生成时提示“Resource.Drawable”未包含“BG”的定义

    Xamarin Android提示Resource.Drawable”未包含“BG”的定义错误信息:error CS0117: '“Resource.Drawable”未包含“BG”的定义Xamari ...

  10. nginx中部署前端,后端打成jar包运行

    项目是前后端分离:前端用vue开发,后端用的是springboot开发 会产生跨域问题,故在前端里用了代理 1.本前端项目是用vue开发: 1.1打包:终端 vscode快捷键:crtl+~  然后n ...