51nod 1182 完美字符串
来源:互联网 发布:mac winebottler msi 编辑:程序博客网 时间:2024/05/17 00:05
这道题思想很简单就是贪心,不过其中遇到比较重要的地方是用到了map的排序(定义一个vector存放map,然后对vector排序),所以做个笔记好让自己回来复习。
#include<cstring>#include<string>#include<cstdio>#include<map>#include<algorithm>#include<vector>#include<iostream>using namespace std;string s;map<char,int> m;typedef pair<char,int> PAIR;///先定义一个pairint ans,t;struct compare{///结构体重写比较函数 bool operator()(const PAIR& lhs,const PAIR& rhs){ return lhs.second>rhs.second; }};int main(){ s.resize(10000); while(scanf("%s",&s[0])!=EOF){ m.clear(); ans=0; t=26; for(int i=0;i<s.length();i++){ if(s[i]>='a') m[s[i]]++; else if(s[i]>='A') m[s[i]+32]++; } vector<PAIR> str(m.begin(),m.end());///vector的快速初始化方式,学习了 sort(str.begin(),str.end(),compare()); for(int i=0;i<str.size();i++){ ans=ans+str[i].second*t--; } printf("%d\n",ans); str.clear(); s.clear(); s.resize(10000); } return 0;}
阅读全文
0 0
- 51NOD 1182 完美字符串
- 51nod 1182 完美字符串
- 51 nod 1182 完美字符串
- 51nod 1182 完美字符串
- 51nod 1182完美字符串
- 51nod 1182 完美字符串
- 51Nod 1182 完美字符串
- 【51Nod】1182 完美字符串
- 51nod 1182 完美字符串
- 51Nod 1182 完美字符串
- 51nod 1182 完美字符串
- 51nod-完美字符串
- 51nod 完美字符串
- 51 Nod 完美字符串
- 51Nod-1182-完美字符串
- 【51Nod】1182 - 完美字符串(贪心)
- 51nod 1182 完美字符串【贪心】
- 51nod 1182 完美字符串(贪心)
- Arduino连接nRF24L01无线收发模块
- 微信小程序-兼容
- [CNN]关于cnn一些经验和理解
- 以此篇文章勉励自己努力学习,致当年对学习不努力的自己——2017-11-12
- shell 数组
- 51nod 1182 完美字符串
- 微信小程序初探
- Volley原理解析
- (译)网站加速最佳实践——雅虎35条
- 运行WordCount案例
- 南华新生训练2017-最长回文子串
- 文章标题
- 大北京的实习生活
- MongoDB介绍、安装及mongocxx C++驱动程序的安装