สมมติว่าเรามีสตริง S ที่มีอักขระ n ตัว S คือคำที่คั่นด้วยช่องว่างเดียว ซึ่งประกอบด้วยตัวอักษรภาษาอังกฤษตัวเล็กและตัวใหญ่ ปริมาณของคำคือจำนวนตัวพิมพ์ใหญ่ในคำที่กำหนด และปริมาณของข้อความคือปริมาณสูงสุดของคำทั้งหมดในข้อความ เราต้องหาปริมาตรของข้อความที่ให้มา
ขั้นตอน
เพื่อแก้ปัญหานี้ เราจะทำตามขั้นตอนเหล่านี้ -
ans := 0 a := 0 n := size of S for initialize i := 0, when i <= n, update (increase i by 1), do: s := S[i] if s >= 'A' and s <= 'Z', then: (increase a by 1) if s is same as blank space, then: ans := maximum of ans and a a := 0 ans := maximum of ans and a return ans
ตัวอย่าง
ให้เราดูการใช้งานต่อไปนี้เพื่อความเข้าใจที่ดีขึ้น -
#include <bits/stdc++.h>
using namespace std;
int solve(string S){
int ans = 0, a = 0;
int n = S.size();
for (int i = 0; i <= n; i++){
char s = S[i];
if ((s >= 'A') && (s <= 'Z'))
a++;
if (s == ' '){
ans = max(ans, a);
a = 0;
}
}
ans = max(ans, a);
return ans;
}
int main(){
string S = "Paper MILL";
cout << solve(S) << endl;
} อินพุต
"Paper MILL"
ผลลัพธ์
4