How to use C++ String in Competition
Today, we are going to talk about C++ String and how to use it in competition. Since it is my first blog about USACO or CCF(In China) competition. And also It works for Computer Science students in USA. This article will divide as two parts. Each part will explain char and String in C or C++.
Char in C
Basic info
Remember there is no String in C! String belong to C++’s library. Now you see the privilege of C++.
Declare:
1 | char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};//char end with '\0' which Means NULL |
Warning: You need to be awared that when to use‘’, and when to use“”.
Here is how it is stored in computer, Warning: Count start with 0!!!
![](String In C and C++/string_representation.jpg)
Printout
1 | printf("%s", greeting );//"Hello" will be print |
Six functions about Char
Here is the example for strcpy and strcat and strlen, and I will explain in the Annotation
1 |
|
Here is the output:
1 | strcpy( str3, str1) : Hello |
Remeber to be careful about the size of the array!
And here are three other functions which would be a little compacte
strcmp
Use: Compare 2 strings if they are same or not, same return 0
Explain: The function compare their ascil number, from the first index of the string to the end, if their ascil number equal then continue, if they are not equal then return their difference.
1 |
|
FAQ Ascil chart
![](String In C and C++/Ascii.png)
Remember ‘a’=97, ‘A’=65.
strchr and strstr
They looks like brother to me. The function strchr(str1,ch) return an array point to the place where ch first appear in str1. And The function strctr(str1,str2) return an array point to the place where str2 first appear in str1.
Code
1 |
|
Reminder: Those function will keep the string we are searching!
String in C++
We can treat C++ as the son of C, so it inherits all the libaray from C. Which means that all the things above is also available for C++. But as the son of C, C++ have some new extensions that are not available in C.
String in C++ is much more easy to understand. It was as the same level as int and long long, which means we can add the strings!
1 |
|
Examples and Solutions!
Warning: those are not for beginners!
I chose the question from USACO 1.3.3 Calf Flac
Here is the question:
1 | It is said that if you give an infinite number of cows an infinite number of heavy-duty laptops (with very large keys), that they will ultimately produce all the world’s great palindromes. Your job will be to detect these bovine beauties. |
SAMPLE INPUT (file calfflac.in)
1 | Confucius say: Madam, I'm Adam. |
OUTPUT FORMAT
The first line of the output should be the length of the longest palindrome found. The next line or lines should be the actual text of the palindrome (without any surrounding white space or punctuation but with all other characters) printed on a line (or more than one line if newlines are included in the palindromic text). If there are multiple palindromes of longest length, output the one that appears first.
SAMPLE OUTPUT (file calfflac.out)
1 | 11 |
Solution
This question takes no hard thinking. Because the data is small(20,000Index) so that we try every single index of the sequence. Then it should be divided into two cases.
- Parlindrome’s length is odd
- Parlindrome’s length is even
And we have a varible to save the length every time we found a new palindrome. At the end we just need to print if out. Here is the code:
1 |
|
Hahhhhh?
You don’t understand some code segment????????
Sure. I will change those with the way that’s easier for you.
1 | for(int i=1;x-i>=0&&i+x<l&&m[x-i]==m[x+i];i++)a1+=2;//Get 1 from middle, length is odd |
Can be write in this way:
1 | for(int i=1;i<=x;i++){ |
And for those kind of code:
1 | if(isalpha(k[i])){//Judge ifk[i]is alpha |
If you don’t know those functions, we can actually write it ourslves.
1 | int h=(int)k[i]; |
Conclusion
Question around String won’t be so hard. As long as we use the function properly. The best way for you to improve is not just to read this blog, but to exercise.
Here is a joke about programming:
In my opinion, C is a girl. Here are the reasons:
No matter what you want it to do, she will never find a way herself.
Use complex method for simple problem
Can never realize the mistake she made herself
It is normal to waste time on her
As long as you have a mistake, she will find out all of a sudden
After all, it is your fault
Please do not take this joke seriously!
Reference Link
1.http://itfun.objweb.com/?paged=10(Joke)
2.https://frostpines.wordpress.com/2010/11/08/usaco-1-3-3-calf-flac/(USACO problem)
3.https://www.runoob.com/cplusplus/cpp-basic-input-output.html