[英]Why the code isn't working and giving wrong output?
we are given a string s and length n.我们得到一个字符串 s 和长度 n。 We have to print the sum of (no. of every character - given i).我们必须打印(每个字符的编号 - 给定 i)的总和。
If we are given c no's of i.we have:如果给我们 c 编号的 i.我们有:
n c
s
i in next lines
input
10 2
aabbaabbcc
1
3
output: output:
7 2 7 2
explain:解释:
7 = 4a's - 1 + 4 b's - 1 + 2c's - 1 7 = 4a's - 1 + 4 b's - 1 + 2c's - 1
code:代码:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int solve(string s,int n){
unordered_map<char,int> m;
int k;
cin>>k;
for(int i=0 ; i< n ;i++){
m[s[i]]++;
}
unordered_map<char, int>:: iterator p;
int sum=0;
for (p = m.begin(); p != m.end(); p++){
p->second -= k ;
if(p->second>0){
sum++;
}
}
return sum;
}
int main() {
// your code goes here
int n , q;
cin>>n>>q;
string s;
cin>>s;
vector<int> r;
for(int i = 1 ; i<=q;i++){
int sb = solve(s,n);
r.push_back(sb);
}
for(int i = 0 ; i<q;i++){
cout<<r[i]<<endl;
}
return 0;
}
Simple mistake简单的错误
if (p->second>0){
sum++;
}
should be应该
if (p->second > 0) {
sum += p->second;
}
You should learn to use a debugger.您应该学习使用调试器。 It would have shown you what the problem was very quickly.它会很快向您显示问题所在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.