求助一道简单题,求大佬帮忙看看,不知道怎么改了
求助一道简单题,求大佬帮忙看看,总显示运行错误:请检查是否存在数组、列表等越界非法访问,内存非法访问等情况
我的代码:
# include<bits/stdc++.h>
using namespace std;
bool cmp(int x,int y)
{
return x>y;
}
int main()
{
//读取字符串
int n;
cin>>n;
cin.get();
string str;
while(getline(cin,str))
{
map<char,int> mp;
int len = str.size();
//将字符串中的每个字符都放入map容器中,并统计个数
for(int j = 0;j<len;j++)
{
char key = str[j];
if(mp.find(key) == mp.end())
{
mp[key] = 1;
}
else mp[key]++;
}
//将字符的个数放入vec容器,并由大到小排序
int mlen = mp.size();
vector<int> vec;
int k = 0;
for(auto pos =mp.begin();pos !=mp.end(); pos++)
{
vec[k] = (*pos).second;
k++;
}
//计算漂亮度
sort(vec.begin(), vec.end(),cmp);//sort默认升序排列
int sum = 0;
int count = 26;
for(int b = 0; b < k; b++)
{
sum += vec[b]*count;
count--;
}
cout<<sum<<endl;
mp.clear();
vec.clear();
}
return 0;
}
using namespace std;
bool cmp(int x,int y)
{
return x>y;
}
int main()
{
//读取字符串
int n;
cin>>n;
cin.get();
string str;
while(getline(cin,str))
{
map<char,int> mp;
int len = str.size();
//将字符串中的每个字符都放入map容器中,并统计个数
for(int j = 0;j<len;j++)
{
char key = str[j];
if(mp.find(key) == mp.end())
{
mp[key] = 1;
}
else mp[key]++;
}
//将字符的个数放入vec容器,并由大到小排序
int mlen = mp.size();
vector<int> vec;
int k = 0;
for(auto pos =mp.begin();pos !=mp.end(); pos++)
{
vec[k] = (*pos).second;
k++;
}
//计算漂亮度
sort(vec.begin(), vec.end(),cmp);//sort默认升序排列
int sum = 0;
int count = 26;
for(int b = 0; b < k; b++)
{
sum += vec[b]*count;
count--;
}
cout<<sum<<endl;
mp.clear();
vec.clear();
}
return 0;
}