n组字母和最大
字母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;
}