Why do these 2 codes differ?

There is a sequence of colorful stones. The color of each stone is one of red, green, or blue. You are given a string s. The i-th (1-based) character of s represents the color of the i-th stone. If the character is "R", "G", or "B", the color of the corresponding stone is red, green, or blue, respectively.

Initially Squirrel Liss is standing on the first stone. You perform instructions one or more times.

Each instruction is one of the three types: "RED", "GREEN", or "BLUE". After an instruction c, if Liss is standing on a stone whose colors is c, Liss will move one stone forward, else she will not move.

You are given a string t. The number of instructions is equal to the length of t, and the i-th character of t represents the i-th instruction.

Calculate the final position of Liss (the number of the stone she is going to stand on in the end) after performing all the instructions, and print its 1-based position. It is guaranteed that Liss don't move out of the sequence.

Input
The input contains two lines. The first line contains the string s (1 ≤ |s| ≤ 50). The second line contains the string t (1 ≤ |t| ≤ 50). The characters of each string will be one of "R", "G", or "B". It is guaranteed that Liss don't move out of the sequence.

Output
Print the final 1-based position of Liss in a single line.

Examples
input
RGB
RRR
output
2
input
RRRBGBRBBB
BBBRR
output
3
input
BRRBGBRGRBGRGRRGGBGBGBRGBRGRGGGRBRRRBRBBBGRRRGGBBB
BBRBGGRGRGBBBRBGRBRBBBBRBRRRBGBBGBBRRBBGGRBRRBRGRB
output
15


For such a problem, I tried 2 codes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <bits/stdc++.h>


#define fl(n)    for(int i = 0; i < n; i++)

#define ll   long long
#define nl   endl
#define pb   push_back
#define mp   make_pair
#define PII  pair<int,int>

#define EPS  1e-9
#define INF  1e9


using namespace std;

int main()
{
    string s, t;

    int index = 0;
    cin >> s;
    cin >> t;
    int in = 1;
    for(int i = 0; i < s.length()-1; i++)
    {
        for(int j = index; j < t.length(); j++)
        {
            if(t[j] == s[i]){
                j++;
                index = j;
                in++;
                break;
            }
        }
    }
    cout << in;

    return 0;
}


This code works in most solutions and fails in some and idk why, even though I traced it but still don't know why the output differs, even in trace.

Take this input as a sample:
BRRBGBRGRBGRGRRGGBGBGBRGBRGRGGGRBRRRBRBBBGRRRGGBBB
BBRBGGRGRGBBBRBGRBRBBBBRBRRRBGBBGBBRRBBGGRBRRBRGRB


The above code outputs 16, while the answer is 15

Meanwhile this code,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <bits/stdc++.h>


#define fl(n)    for(int i = 0; i < n; i++)

#define ll   long long
#define nl   endl
#define pb   push_back
#define mp   make_pair
#define PII  pair<int,int>

#define EPS  1e-9
#define INF  1e9


using namespace std;

int main()
{
    string s, t;

    cin >> s;
    cin >> t;
    int in = 1, j = 0;
    for(int i = 0; i < t.length(); i++)
    {
        if(t[i] == s[j]){
                j++;
                in++;
            }
    }
    cout << in;

    return 0;
}


Never has a wrong answer, what's the difference? I see them both the same just diff in loops :/ can someone explain that?
Last edited on
rgb
g

correct answer: 1
first code output: 2.
compare lines 28 - 36 in the first sample with line 27 - 30 in the second.

In the second program, you are comparing the instruction (t[i]) against the current location (s[j]). If it matches, you move your current location 1 spot.

In the first program you loop through all of the locations from the current (j = index) to the end (j < t.length()) and compare the instruction against each location. If any of them are the correct color, you move your current location 1 spot.
Thanks, realized I am dumb xD
Topic archived. No new replies allowed.