📜  门| GATE CS 1996 |第75章(1)

📅  最后修改于: 2023-12-03 15:42:10.514000             🧑  作者: Mango

GATE CS 1996 Question 75

This question is from the GATE CS 1996 exam and it concerns the topic of programming.

Question

Suppose we are given a language L as a set of strings which has the following properties:

  1. There is a string s $\in$ L, called the separator, such that every string in L can be written as $\alpha s \beta$ where $\alpha, \beta$ are strings not containing s, and $\alpha^R$ is not a substring of $\beta$ for any $\alpha, \beta$.

  2. There is an algorithm RA which, given an input string x, either determines that x is not a member of L, or finds a string y in L such that y is a subsequence of x.

Show that there is an algorithm RB which given an input string x, either determines that x is not a member of L, or finds a string y in L such that y is a substring of x.

Solution

To solve this problem, we need to find an algorithm RB that satisfies the requirements specified in the question. We can do this by transforming algorithm RA to algorithm RB.

Firstly, note that if algorithm RA finds a subsequence y of x, then it is also a substring of x. So if algorithm RA finds a string y in L that is a subsequence of x, then we can output y as the answer for algorithm RB.

Secondly, if algorithm RA determines that x is not a member of L, then we can simply output the same result in algorithm RB. This will ensure that algorithm RB satisfies the first requirement of the question.

Now we need to consider the case where algorithm RA finds a string y in L that is not a subsequence of x. In this case, we have to modify y so that it becomes a substring of x. We can do this by inserting separator s in y appropriately.

Let $y = \alpha s \beta$ where $\alpha, \beta$ are strings not containing s. Since y is not a subsequence of x, there must exist a substring $z \neq y$ of x such that $\alpha s$ is a substring of z and $\beta$ is a substring of z. We can find z by scanning through x and finding the first occurrence of $\alpha s$ and the first occurrence of $\beta$ after that. We can then construct a new string $y' = \alpha s w \beta$ where $w$ is the substring of z between $\alpha s$ and $\beta$. This string $y'$ is a substring of x and is also in L.

We can summarize the algorithm RB as follows:

  1. Run algorithm RA on input string x.
  2. If algorithm RA outputs "not a member of L", output the same result and stop.
  3. If algorithm RA outputs a subsequence y of x, output y and stop.
  4. If algorithm RA outputs a string y not in L, find z as described above and construct y' as described above. Output y' and stop.

Therefore, we have shown that there exists an algorithm RB that satisfies the requirements specified in the question.

## Solution

To solve this problem, we need to find an algorithm RB that satisfies the requirements specified in the question. We can do this by transforming algorithm RA to algorithm RB.

Firstly, note that if algorithm RA finds a subsequence y of x, then it is also a substring of x. So if algorithm RA finds a string y in L that is a subsequence of x, then we can output y as the answer for algorithm RB.

Secondly, if algorithm RA determines that x is not a member of L, then we can simply output the same result in algorithm RB. This will ensure that algorithm RB satisfies the first requirement of the question.

Now we need to consider the case where algorithm RA finds a string y in L that is not a subsequence of x. In this case, we have to modify y so that it becomes a substring of x. We can do this by inserting separator s in y appropriately.

Let $y = \alpha s \beta$ where $\alpha, \beta$ are strings not containing s. Since y is not a subsequence of x, there must exist a substring $z \neq y$ of x such that $\alpha s$ is a substring of z and $\beta$ is a substring of z. We can find z by scanning through x and finding the first occurrence of $\alpha s$ and the first occurrence of $\beta$ after that. We can then construct a new string $y' = \alpha s w \beta$ where $w$ is the substring of z between $\alpha s$ and $\beta$. This string $y'$ is a substring of x and is also in L.

We can summarize the algorithm RB as follows:

1. Run algorithm RA on input string x.
2. If algorithm RA outputs "not a member of L", output the same result and stop.
3. If algorithm RA outputs a subsequence y of x, output y and stop.
4. If algorithm RA outputs a string y not in L, find z as described above and construct y' as described above. Output y' and stop.

Therefore, we have shown that there exists an algorithm RB that satisfies the requirements specified in the question.