“This is the 23rd day of my participation in the First Challenge 2022. For details: First Challenge 2022”
I. Problem description
The 19-letter sequence abcDEFghijklMNOpQrs was repeated 106 times to obtain a string of length 2014.
Then delete the first letter (the beginning letter A), the third letter, the fifth letter, and all odd-numbered letters.
The new string is then deleted from the odd-position letter action. This continues until there is only one letter left. Please print that letter.
Two, the title requirements
inspection
1. Functions related to strings and their usage 2. The recommended time is 15 to 25 minutesCopy the code
Operating limits
- Maximum running time: 1s
- Maximum running memory: 128M
Third, problem analysis
The C++ STL library provides many functions for strings, as described in the summary and improvement section below.
At first, I used the for loop to determine if it was prime and delete the corresponding letter, but then I realized, for example, that if you delete the first element, when you delete the third element, the third element becomes the second, and so on, the fifth element becomes the third, and the seventh element becomes the fourth……
It’s about deleting 1 to n elements until the last one is left. It’s easy once you find the pattern.
Four, coding implementation
#include <iostream>
#include<string>/ / header files
using namespace std;
int main(a)
{
string s1="abcdefghijklmnopqrs";// Initializes the string
string s;
int i,j,n=106;// add 106
for(i=0; i<n; i++) { s+=s1; }while(s.size()! =1)// Enter the loop when it is not the last one, exit the loop otherwise
{
for(i=0; i<s.size(a); i++) { s.erase(i,1);// Delete elements
}
}
cout<<s;// Output the result
return 0;
}
Copy the code
5. Output results
The output is q
Sixth, summary and improvement
In C++, the C++ standard library provides the string class type, which supports all of the above operations, and adds many more features, such as adding headers to programming:
#include<bits/stdc++. H > using namespace STD;Copy the code
The String functions:
Use string to define class s (you can define anything, just change s to the defined letter to call C++ functions).
function | usage |
Common operation | |
s.resize(10) | Set the string length to 10 |
string s(“ABC”) | Construct the string s with the value ABC |
s.empty() | Checks if the string is empty |
S.l ength () or s.s considering () | Finding string length |
s.begin() | Begin to value |
s.end() | At the end of the value |
Find (returns element location on success, -1 on failure) | |
s.find(‘A’) | Find character A |
s.find(“ABC”) | Find the string ABC |
s.find(‘A’,2) | Look for character A starting at position 2 |
S. ind (” ABCD “, 1, 2) | Starting at position 1, look for the first two characters of ABCD |
s.rfind() | Start at the end of the string |
insert | |
S.i nsert (2, 3, ‘A’) | Add three A’s at subscript 2 |
s.insert(2,”ABC”) | Add the string ABC at subscript 2 |
s.insert(2,”ABC”,1) | Add one of ABC’s at subscript 2 |
S.i nsert (2, “ABCD”, 2, 2) | 2 characters at subscript 2 from position 2 in the string ABCD |
s.push_back(‘A’) | Add the character A to the end |
The output | |
s.substr(pos,len) | Pos represents the subscript of the output character and len represents the length |
replace | |
S.r eplace (2, 4, “ABCD”) | Replace 4 characters from subscript 2 with “ABCD” |
delete | |
s.erase(2) | Delete all elements after subscript 2 |
S.e rase (2, 1) | Deletes an element with subscript 2 |
flip | |
reverse(s.begin(),s.end()) | Reverse all strings, that is, output in reverse order |
copy | |
s1=s.substring(2) | Extract the string s from 2 to the tail and assign it to S1 |
S1 = s.s ubstring (2, 3) | Extract the string s starting at 2 and assign three characters to S1 |
const char*s1=s.data() | Turn the String class into an array of strings |
S.c opy (s1, 2, 3) | Copy 2 characters from position 3 in s into s1 |
Compare (assuming the original string is ABCD) | |
s.compare(“ABCD”) | Equal returns 0, greater than the original string returns 1, less than -1 |
empty | |
s.assign(“ABC”) | Empty the string and set it to ABC |
s.assign(“ABC”,2) | Empty the string and concatenate the first 2 characters AB of ABC |
S.a ssign (” ABC “, 2, 1) | Empties the string to 1 character starting at 2 for ABC |
S.a ssign (5, ‘A’) | Empty the string and juxtapose it to five as |
s.clear() | Clears the string of all characters |