Analysis for POJ 3302
Problem meaning:
Give two strings S1 and S2 to judge whether S2 or the reverse string of S2 is a substring of S1. From the example, the substring does not need to be continuous in S1, as long as the sequence is right.
Input: (1) integer T, there are several test cases; (2) next t line, each line has two strings;
Output: t line yes / no.
First Solution: use lists to process these characters.
First we start with the left to right order.
- Define 2 lists, a[10000] and b[10000]. a[10000] is S1, and b[10000] is S2.
- Make 2 loops, the first loop starts at 0 and ends at the length of list b.
Which is for(i=0;i<strlen(b);i++)
At here, we are going to test whether the variables in list b are all included in list a. - The second loop starts at a variable and ends at the length of list a.
Which is for(j=k;j<strlen(a);j++)
Inside the loop, we have to make sure whether each variable in list b is included in list a, which is
if(b[i]==a[j])
{
c = 1;
break;
}
C is a Boolean variable, which before the second loop we set c to be 0.
If b[i] equals a[j], we quit the second loop for saving time.
Here explains the variable k. After we “resume” the second loop, we do not wish to start over again from 0 or we will lose the order, because the list b should be placed in order in list a. In order to keep the progress, we use
k = j + 1;
If b[i] does not equal a[j], we continue the second loop, and set c to be 1 until b[i] equals a[j]. If after the second loop and c is still 0, it means b[i] is not the right order or not included in list a, then we quit the first loop, which means list b is not in the right order of list a from left to right.
Second, we try whether the reverse order of list a works.
- The only difference is the programming the reverse order of list a,
Which is int count=0;
for(i=strlen(a)-1;i>=0;i–)
{
d[count]=a[i];
count++;
} - If c still equals to 0 after the second loop, then S2 is not a substring of S1.
Second solution: use of C++ algorithms.
1、string::size_ Type describes size
Note that if you want to use string to define the variable, you can’t use string to define the variable_ type.
-
String:: NPOs: is a constant used to indicate a nonexistent position. It can be directly used as a number, or as str1.npos.
-
string.find ():
(1) The return value is the position (subscript) of a letter or a continuous string in the mother string. If it is not found, a special token NPOs is returned. (the return value can be regarded as an int type number)
string s;
string::size_ type position;
Position = s.find (“JK”); / / the find function returns the subscript position of JK (or a single letter) in S
if (position == s.npos) {… }
If (position = = string:: NPOs)
if (position != s.npos) {… }
Can’t find.
(2) Find from specified location
Pos1 = STR1. Find (C, POS); / / search from the position of POS variable value
Pos1 = STR1. Find (C, 5); / / start from 5 -
string.earse ()
The erase() function of the string class is used to delete the character sequence
For example: str.erase (10, 8), delete the string with length of 8 starting from the 10th bit; -
string.size ()、 string.begin ()、 string.end (), which represents the length, first or last character of the string;
-
The reverse() function reverses strings and arrays of any type.