字母A-J,用0-9对应字母使得n组数据和最大,输入字符串前面保证非0

如输入组数据:

2

ABC

BCA

输出:

1875

思路:其实就是求和,对应字符乘以相应的量级,按系数排序

如上MAX(101A+110B+11C)

那B:9 A:8,C:7产生最大和

同时考虑类型为字符串涉及字符串加法

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <math.h>
#define IMIN numeric_limits<int>::min()
#define IMAX numeric_limits<int>::max()
#define FR(i,n) for(int i=0;i<n;i++)
#define CLC(x) memset(x,0,sizeof(x))
#define FILL(x,c) memset(x,c,sizeof(x))
#define viter vector<int>::const_iterator
using namespace std;
string invert(string &src)
{
string newStr=src;
for(int i=src.length()-1,j=0; i>=0; --i,++j)
newStr[j]=src[i];
return newStr;
}
void inverse(string &s)
{
for(int i=0;i<s.size()/2;++i){
char tmp= s[i];
s[i] = s[s.size()-1-i];
s[s.size()-1-i] = tmp;
}
}
string intAdd(string &rs1,string &rs2)
{
string str1=invert(rs1);
string str2=invert(rs2);
if(str1.length()<str2.length())
str1.swap(str2); for(size_t i=0; i!=str2.length(); ++i)
{
char c1=str1[i];
char c2=str2[i];
int t=((int)c1-48)+((int)c2-48);
if(t>=10)
{
//进位
int x=t/10;
t%=10;
size_t n=i+1;
do
{
int t1=(int)str1[n]-48+x;
if(t1>=10)
{
str1[n]=(char)(t1%10+48);
++n;
}
else
{
str1[n]=(char)(t1+48);
}
if(n==str1.length())
{
str1+="1";
break;
}
x=t1/10;
}
while(x!=0); str1[i]=(char)(t+48);
}
else
{
str1[i]=(char)(t+48);
}
}
string &rstrResult=str1;
string strOut=invert(rstrResult);
return strOut;
} void init_map(map<char,double> &wmap)
{
for(char i='A';i<'K';++i)wmap[i]=0.1;
}
string get_max(vector<string> &vstr)
{
map<char,double> wmap;
init_map(wmap);
for(int i=0;i<vstr.size();++i)
{
double count =1.0;
for(int j=0;j<vstr[i].size();++j)//0对应个位,逆序
{ wmap[vstr[i][j]]=wmap[vstr[i][j]]+count;
count = count*10;
}
}
map<double,char> nmap;
map<char,double>::iterator it = wmap.begin();
while(it!=wmap.end()){
//cout<<"1="<<it->first<<" "<<it->second<<endl;
if(nmap.count(1.0/double(it->second))){
double dtmp=it->second-0.1;
while(nmap.count(1.0/double(dtmp)))dtmp=dtmp-0.1;
nmap[1.0/double(dtmp)]=it->first;
//cout<<"SEC1: "<<1.0/double(dtmp)<<"fir1:"<<it->first<<endl;
}
else {
//cout<<"SEC2: "<<1.0/double(it->second)<<" fir2:"<<it->first<<endl;
nmap[1.0/double(it->second)]=it->first;
}
it++;
}
map<char,char> idmap;
map<double,char>::iterator it0 = nmap.begin();
char num='9';
while(it0!=nmap.end()){
idmap[it0->second]=char(num);
cout<<idmap[it0->second]<<": "<<it0->second<<endl;
num = num-1;
it0++;
}
string res;
for(int i=0;i<vstr.size();++i)
{
inverse(vstr[i]);
for(int j=0;j<vstr[i].size();++j)
{
vstr[i][j]=idmap[vstr[i][j]];
}
cout<<vstr[i]<<endl;
res=intAdd(res,vstr[i]);
}
// inverse(res);
return res;
} int main()
{ int n;
while(cin>>n)
{
vector<string> vstr;
FR(i,n){
string stmp;
cin>>stmp;
inverse(stmp);
vstr.push_back(stmp);
}
cout<<get_max(vstr)<<endl;
}
return 0;
}

n组字母和最大的更多相关文章

  1. 信息安全-2:python之hill密码算法[原创]

    转发注明出处:http://www.cnblogs.com/0zcl/p/6106513.html 前言: hill密码算法我打算简要介绍就好,加密矩阵我用教材上的3*3矩阵,只做了加密,解密没有做, ...

  2. Ubuntu下用NdisWrapper安装网卡驱动

    下面是一个简单全面的使用NdisWrapper的指南.这是从Beginning Ubuntu Linux, Second Edition中提炼出来的. 这份指南是第8章的一部分.该章给出了在Ubunt ...

  3. java 正则

    ava - 正则表达式 - Pattern - Matcher 2013-08-21 14:35 3325人阅读 评论(0) 收藏 举报  分类: JavaSE(30)  版权声明:本文为博主原创文章 ...

  4. Unique Morse Code Words

    Algorithm [leetcode]Unique Morse Code Words https://leetcode.com/problems/unique-morse-code-words/ 1 ...

  5. 【ARTS】01_07_左耳听风-20181224~1230

    ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...

  6. leetcode笔记(五)809. Expressive Words

    题目描述 Sometimes people repeat letters to represent extra feeling, such as "hello" -> &qu ...

  7. js面试之一个字符串中出现次数最多的字符是?出现几次?

    最近在找面试题的时候发现了许多有趣的题目,在这里用随笔记录下~ 关于“一个字符串中出现次数最多的字符...”这种问题在笔试题中出现的频率还是很高的,我自己也找到了几种方法处理 var str = &q ...

  8. 院校-美国:哈佛大学(Harvard University)

    ylbtech-院校-美国:哈佛大学(Harvard University) 哈佛大学(Harvard University),简称“哈佛”,坐落于美国马萨诸塞州波士顿都市区剑桥市,是一所享誉世界的私 ...

  9. HPU暑期集训积分赛2

    A. 再战斐波那契 单点时限: 1.0 sec 内存限制: 512 MB 小z 学会了斐波那契和 gcd 后,老师又给他出了个难题,求第N个和第M个斐波那契数的最大公约数,这可难倒了小z ,不过在小z ...

随机推荐

  1. spring cloud config的使用

    在传统的应用中,我们的配置文件都是放在项目中,这个影响不大.但是在一个微服务架构的系统中,我们的微服务可能存在几十上百个,并且每个小的微服务可能又部署在多台机器上,那么这个时候如果我们的配置文件在都放 ...

  2. stm32学习心得体会

    stm32作为现在嵌入式物联网单片机行业中经常要用多的技术,相信大家都有所接触,今天这篇就给大家详细的分析下有关于stm32的出口,还不是很清楚的朋友要注意看看了哦,在最后还会为大家分享有些关于stm ...

  3. python re:正向肯定预查(?=)和反向肯定预查(?<=)

    参考资料:https://tool.oschina.net/uploads/apidocs/jquery/regexp.html (?=pattern) 正向肯定预查,在任何匹配pattern的字符串 ...

  4. DeWeb --- Hello,World!

    1.新建一个DLL,命名为hello.dpr 2.新增一个Form.(File->New->VCL Form - Delphi),建议不要更改单元名称和Form名称,即分别为unit1.p ...

  5. 攻防世界 Misc 新手练习区 stegano CONFidence-DS-CTF-Teaser Writeup

    攻防世界 Misc 新手练习区 stegano CONFidence-DS-CTF-Teaser Writeup 题目介绍 题目考点 隐写术 摩斯密码 Writeup 下载附件是PDF文件打开,研究一 ...

  6. 攻防世界 Misc 新手练习区 give_you_flag Writeup

    攻防世界 Misc 新手练习区 give_you_flag Writeup 题目介绍 题目考点 gif图片分离 细心的P图 二维码解码 Writeup 下载附件打开,发现是一张gif图片,打开看了一下 ...

  7. springmvc学习笔记(全)

    SpringMVC简介 什么是MVC MVC是一种软件架构的思想,将软件按照模型.视图.控制器来划分 M: Model:模型层,指工程中的JavaBean,作用是处理数据.JavaBean分为两类: ...

  8. hudi clustering 数据聚集(一)

    概要 数据湖的业务场景主要包括对数据库.日志.文件的分析,而管理数据湖有两点比较重要:写入的吞吐量和查询性能,这里主要说明以下问题: 1.为了获得更好的写入吞吐量,通常把数据直接写入文件中,这种情况下 ...

  9. Effective Python(3)- 了解 bytes 与 str 的区别

    Python 有两种类型可以表示字符序列 bytes:实例包含的是原始数据,即 8 位的无符号值(通常按照 ASCII 编码标准来显示) str:实例包含的是 Unicode 码点(code poin ...

  10. 问题 B: 比大小

    题目描述 给你两个很大的数,你能不能判断出他们两个数的大小呢? 比如123456789123456789要大于-123456 输入 每组测试数据占一行,输入两个不超过1000位的10进制整数a,b 数 ...