#5. 字符串查找与字符提取
字符串查找与字符提取
Extract Characters Around Word
Background
In a string processing scenario, you need to identify the occurrences of a specific word within a long string and extract the characters immediately before and after these words.
Problem Description
Given a main string and a non-empty word string, return a new string composed of the character immediately preceding and the character immediately following each appearance of the given word in the main string. Ignore cases where there is no character before or after the word. A character may be included twice if it is positioned between two occurrences of the word.
Input Format
Input is given from standard input in the following format.
S W
Where S is the main string and W is the word string. S and W are separated by a space.
Output Format
Output is printed to standard output in the following format.
result_string
Where result_string is the new string composed of extracted characters according to the problem requirements.
Sample
abcXY123XYijk XY
c13i
XY123XY XY
13
XY1XY XY
11
Sample Explanation
Sample 1:
Main string S = "abcXY123XYijk", word string W = "XY".
- The first
XYappears at index 3. The character before it is 'c', and the character after it is '1'. Current result: "c1". - The second
XYappears at index 8. The character before it is '3', and the character after it is 'i'. Final result: "c13i".
Sample 2:
Main string S = "XY123XY", word string W = "XY".
- The first
XYappears at index 0. There is no character before it. The character after it is '1'. Current result: "1". - The second
XYappears at index 5. The character before it is '3'. There is no character after it. Final result: "13".
Sample 3:
Main string S = "XY1XY", word string W = "XY".
- The first
XYappears at index 0. There is no character before it. The character after it is '1'. Current result: "1". - The second
XYappears at index 3. The character before it is '1'. There is no character after it. Final result: "11".
Constraints
Time limit: 1 second, Memory limit: 1024 KiB for each test case.