AcWing 5370. 最小和
原题链接
中等
作者:
chn_0kr
,
2023-12-11 18:26:29
,
所有人可见
,
阅读 103
当你什么都忘记了只记得stl时:)
C++ 代码
#include<bits/stdc++.h>
using namespace std;
const int N=20;
const int M=1e4;
int p[N];
bool st[M];//若是第一个字母 则不分配0
typedef pair<int,char> PIC;
int main()
{
int n; cin>>n;
vector<PIC> temp;
for(int i=0; i<10; i++) {temp.push_back({0,'a'+i});}
p[0]=1;//数位原理 每个位置的初始化
for(int i=1; i<7; i++)
{
p[i]=p[i-1]*10;
}
while (n--)
{
string a; cin>>a;
reverse(a.begin(),a.end());
for(int i=0; i<a.size(); i++)
{
if(i==a.size()-1) st[a[i]]=1;
for(int j=0; j<10;j++)
{
if(a[i]==temp[j].second) temp[j].first+=p[i];
}
}
}
sort(temp.begin(),temp.end());
reverse(temp.begin(),temp.end());
int res=0;
int cnt;
for(int i=0; i<10; i++)
{
if(!st[temp[i].second])
{
vector<PIC>::iterator it = temp.begin() + i;
temp.erase(it);//将分配0的项直接删除
break;
}
}
for(int i=0; i<9; i++) res+=temp[i].first*(i+1);
cout<<res;
return 0;
}
为什么你记得循环,记得变量,还记得输入输出啊?
长得帅