An English booklet has been created for publicizing Aizu to the world. When you read it…
- User
An English booklet has been created for publicizing Aizu to the world. When you read it carefully, you found a misnomer (an error in writing) on the last name of Masayuki Hoshina, the lord of the Aizu domain. The booklet says "Hoshino" not "Hoshina".
Your task is to write a program which replace all the words "Hoshino" with "Hoshina". You can assume that the number of characters in a text is less than or equal to 1000. Input
The input consists of several datasets. There will be the number of datasets n in the first line. There will be n lines. A line consisting of english texts will be given for each dataset.
Output
For each dataset, print the converted texts in a line.
Example
Input
3 Hoshino Hashino Masayuki Hoshino was the grandson of Ieyasu Tokugawa.
Output
Hoshina Hashino Masayuki Hoshina was the grandson of Ieyasu Tokugawa.
Assistant
We are asked to replace all occurrences of "Hoshino" with "Hoshina" in each input line. The input: first line is number of datasets n. Then n lines of text (English). Each line may contain spaces, punctuation, etc. We need to replace exactly the substring "Hoshino" with "Hoshina". Simple string replacement.
Constraints: length <= 1000. Input lines may contain spaces. Since reading with typical input methods, we need to read the lines including spaces. Use getline after reading integer n. But careful: after reading n with cin, there may be a newline left. So need to consume it before using getline. Or use getline after reading n to consume the newline. The input example includes a line with spaces. So approach: read integer n, then for i from 0 to n-1, read a line using getline. For each line, replace all occurrences.
Replacement: can use simple loop: find "Hoshino", replace. Or use regex replace. But simple manual replace: iterate through string and construct new string. Since length <= 1000, we can just use find and replace repeatedly.
Potential pitfall: overlapping? "Hoshino" doesn't overlap with itself, as replacing "Hoshino" with "Hoshina" which is same length except one char different? Actually "Hoshino" length 7, "Hoshina" length 7? Let's count: H o s h i n o = 7 characters. H o s h i n a = 7 characters. So length unchanged. So we can just replace in-place by scanning and changing o to a? But careful: there might be multiple occurrences; we need to replace the last character from 'o' to 'a'. However, there might be words like "Hoshino" but with case? Problem says english texts; likely proper noun capitalized as "Hoshino". The booklet says "Hoshino" not "Hoshina", so it's specifically the string "Hoshino". We should treat exact case. So we can simply replace every occurrence of the substring.
Simplest: For each line, while (pos = line.find("Hoshino")) != string::npos, line.replace(pos, 7, "Hoshina"). That will work. Or we can manually create result: iterate i; if line.substr(i,7)=="Hoshino", then append "Hoshina" and i+=6; else append character.
Let's implement with find and replace.
Edge Cases: The line may have no occurrence. Also may have multiple.
Implementation in C++:
#include #include using namespace std;
int main() { int n; cin >> n; cin.ignore(); // ignore newline after integer for (int i=0; i<n; i++) { string line; getline(cin, line); size_t pos = 0; while ((pos = line.find("Hoshino", pos)) != string::npos) { line.replace(pos, 7, "Hoshina"); pos += 7; // move past replaced part; but since replaced length same, we can also do pos += 7 to avoid infinite loop. } cout << line << endl; } return 0; }
We need to ensure we treat possible overlapping? Not needed because "Hoshino" length 7, replacement same length, and after replacement the replaced part ends with 'a' not 'o', so it won't form a new "Hoshino" overlapping with previous part. However, consider if the string is "HoshinoHoshino"? after replacing first, second remains. Since we move past, it's fine.
Alternatively, use standard algorithm.
That's it.
But we should verify with example:
Input: 3 Hoshino Hashino Masayuki Hoshino was the grandson of Ieyasu Tokugawa.
Our program should output:
Hoshina Hashino Masayuki Hoshina was the grandson of Ieyasu Tokugawa.
Yes.
We need to be careful about the input format: after reading n, there might be lines that could be empty? Possibly but likely not. If empty lines exist, we should still process them. getline will read empty lines as empty string, which is fine.
Now finalize answer.
But note: the problem statement: "The input consists of several datasets. There will be the number of datasets n in the first line. There will be n lines. A line consisting of english texts will be given for each dataset." So exactly n lines after the first line. So our approach works.
Thus solution is straightforward.
However, in some judges they might expect output exactly as described. So we should output each converted line.
Time complexity: O(n * L) where L <= 1000, trivial.
Now produce final answer.
But we might include more explanation for clarity.
We'll output code.
Let's also consider if they want us to consider overlapping? No.
Thus final answer: C++ code.
But we need to provide the answer in the "final" channel: a program with explanation? The user asked: "Your task is to write a program which replace all the words "Hoshino" with "Hoshina". " So we need to provide the program. In typical ChatGPT response, we provide code and brief explanation. Since this is a coding problem, we can include the code block.
Thus final. ```cpp
#include #include
int main() { int n; std::cin >> n; std::cin.ignore(); // consume the newline after the integer
for (int i = 0; i < n; ++i) { std::string line; std::getline(std::cin, line); size_t pos = 0; // Replace every occurrence of "Hoshino" with "Hoshina" while ((pos = line.find("Hoshino", pos)) != std::string::npos) { line.replace(pos, 7, "Hoshina"); pos += 7; // move past the replaced part } std::cout << line << std::endl; } return 0;}