Analysis for POJ 3302

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.

  1. Define 2 lists, a[10000] and b[10000]. a[10000] is S1, and b[10000] is S2.
  2. 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.
  3. 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.

  1. 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++;
    }
  2. 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.

  1. String:: NPOs: is a constant used to indicate a nonexistent position. It can be directly used as a number, or as str1.npos.

  2. 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

  3. 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;

  4. string.size ()、 string.begin ()、 string.end (), which represents the length, first or last character of the string;

  5. The reverse() function reverses strings and arrays of any type.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值