Kanna is extremely disappointed to find out that no one in his school knows his first name. Even his classmates call him only by his last name. Frustrated, he decides to make his fellow college students know his first name by forcing them to solve this question.
Kanna has given a long string as input in each testcase, containing any ASCII character. Your task is to find out the number of times SUVO and SUVOJIT appears in it.
Input Format
The first line contains the number of testcases, T. Next, T lines follow each containing a long string S.
Output Format
For each long string S, display the no. of times SUVO and SUVOJIT appears in it.
INPUT
5
SUVOJITSUVOSUVOJITUCSUVOJITSUVOVXSUVOJITSUVOGMSUVODMMVDSUVOJIT
AXRSUVOJITHSUVOJITHSUVOJJSUVOJITSUVOJIT
SUVOJITPRQSUVOJIT
SUVOJITTXGSUVOUNSUVOJIT
SUVOJITSUVOSUVOJITXGSUVOSUVOQSUVOJITKDSALASUOUSUVOJITGJEM
OUTPUTSUVO = 4, SUVOJIT = 5
SUVO = 1, SUVOJIT = 4
SUVO = 0, SUVOJIT = 2
SUVO = 1, SUVOJIT = 2
SUVO = 3, SUVOJIT = 4
#include <iostream>
#include<string>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--){
string arr;
cin>>arr;
//cout<<arr;
//cout<<arr.length();
int s=0,sj=0;
for(int i=0;i<=arr.length();i++){
if(arr.substr(i,4)=="SUVO"){
s++;
// cout<<i<<" "<<i+4<<"\n";
}
if(arr.substr(i,7)=="SUVOJIT")
sj++;
}
//cout<<arr.substr(4,4);
cout<<"SUVO = "<<s-sj<<", SUVOJIT = "<<sj<<"\n";
}
return 0;
}
Problem Description
Kanna wants to find out how many times the substrings "SUVO" and "SUVOJIT" appear in each given long string. You need to implement a solution that reads multiple test cases, processes each string to count the occurrences of these substrings, and outputs the results.
Solution Approach
To solve the problem, you need to:
- Read the number of test cases.
- For each test case, read the string and count occurrences of "SUVO" and "SUVOJIT".
- Ensure that overlapping occurrences are counted correctly. For example, in the string "SUVOJITSUVO", "SUVO" and "SUVOJIT" both appear.
Provided Solution Explanation
The provided C++ code reads the number of test cases and for each test case, processes the string to count occurrences of the substrings "SUVO" and "SUVOJIT". Here’s a detailed explanation:
#include <iostream>
#include <string>
using namespace std;
int main() {
int t;
cin >> t; // Read number of test cases
while (t--) {
string arr;
cin >> arr; // Read the long string for each test case
int s = 0, sj = 0;
int length = arr.length();
// Loop through the string
for (int i = 0; i <= length - 1; i++) {
// Check if the substring "SUVO" starts at position i
if (i <= length - 4 && arr.substr(i, 4) == "SUVO") {
s++;
// Skip the length of "SUVOJIT" to avoid counting overlapping cases
// Adjust if needed
}
// Check if the substring "SUVOJIT" starts at position i
if (i <= length - 7 && arr.substr(i, 7) == "SUVOJIT") {
sj++;
}
}
// Print results for each test case
cout << "SUVO = " << s - sj << ", SUVOJIT = " << sj << endl;
}
return 0;
}
Detailed Explanation
Input Handling:
cin >> t;
reads the number of test cases.- For each test case,
cin >> arr;
reads the string.
Substring Counting:
- Loop through the string from index 0 to
length - 1
. - Use
arr.substr(i, 4)
to get the substring of length 4 starting at index i
. - Check if this substring matches "SUVO".
- Similarly, use
arr.substr(i, 7)
to check for "SUVOJIT". - Count occurrences of "SUVO" and "SUVOJIT".
Counting Adjustment:
- Subtract occurrences of "SUVOJIT" from occurrences of "SUVO" to avoid double-counting substrings where "SUVOJIT" includes "SUVO".
Output:
- Print the number of times "SUVO" and "SUVOJIT" appear for each test case.