这段代码的问题在于使用了两个数组来存储输入的字符串,占用了大量的内存空间。可以考虑只用一个数组来存储输入的字符串,然后逐个字符判断是否为 "sos",从而计算出每个人的答案。另外,可以使用一个变量来记录最大的答案,而不需要使用数组来存储每个人的答案。修改后的代码如下:

#include<bits/stdc++.h> using namespace std;

int n, ans = 0; string name, s;

int main() { cin >> n; for (int i = 1; i <= n; i++){ cin >> name >> s; int cnt = 0; for(int j = 0; j < s.size() - 2; j++){ if(s[j] == 's' && s[j+1] == 'o' && s[j+2] == 's') cnt++; } if(cnt > ans){ ans = cnt; cout << name << ' '; } else if(cnt == ans){ cout << name << ' '; } } cout << endl << ans; return 0; }

以下代码出现爆内存1≤n≤100#includebitsstdc++husing namespace std;int na110;string name110 s110;int main cin n; for int i = 1; i = n; i++ cin namei si; int ans = 0; forint i = 1;i = n;i++

原文地址: https://www.cveoy.top/t/topic/bMAJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录