查询字符串的子序列
给定一个字符串S和Q查询,每个查询都包含一个字符串T 。任务是如果 T 是 S 的子序列则打印“是”,否则打印“否”。
例子:
Input : S = "geeksforgeeks"
Query 1: "gg"
Query 2: "gro"
Query 3: "gfg"
Query 4: "orf"
Output :
Yes
No
Yes
No
对于每个查询,使用蛮力,开始迭代 S 寻找 T 的第一个字符。一旦找到第一个字符,就继续迭代 S 现在寻找 T 的第二个字符,依此类推(参考 this for细节)。如果设法找到 T 的所有字符,则打印“是”,否则打印“否”。时间复杂度是O(Q*N),N是S的长度。
如果我们知道 T 的下一个字符在 S 中的位置,那么有效的方法可能是。然后简单地跳过当前和下一个字符字符跳转到该位置。这可以通过制作 |S| 来完成。 x 26 大小的矩阵并存储 S 的每个位置的每个字符的下一个位置。
下面是上述想法的实现:
C++
// C++ program to answer subsequence queries for a
// given string.
#include
#define MAX 10000
#define CHAR_SIZE 26
using namespace std;
// Precompute the position of each character from
// each position of String S
void precompute(int mat[MAX][CHAR_SIZE], char str[],
int len)
{
for (int i = 0; i < CHAR_SIZE; ++i)
mat[len][i] = len;
// Computing position of each character from
// each position of String S
for (int i = len-1; i >= 0; --i)
{
for (int j = 0; j < CHAR_SIZE; ++j)
mat[i][j] = mat[i+1][j];
mat[i][str[i]-'a'] = i;
}
}
// Print "Yes" if T is subsequence of S, else "No"
bool query(int mat[MAX][CHAR_SIZE], const char *str,
int len)
{
int pos = 0;
// Traversing the string T
for (int i = 0; i < strlen(str); ++i)
{
// If next position is greater than
// length of S set flag to false.
if (mat[pos][str[i] - 'a'] >= len)
return false;
// Setting position of next character
else
pos = mat[pos][str[i] - 'a'] + 1;
}
return true;
}
// Driven Program
int main()
{
char S[]= "geeksforgeeks";
int len = strlen(S);
int mat[MAX][CHAR_SIZE];
precompute(mat, S, len);
query(mat, "gg", len)? cout << "Yes\n" :
cout << "No\n";
query(mat, "gro", len)? cout << "Yes\n" :
cout << "No\n";
query(mat, "gfg", len)? cout << "Yes\n" :
cout << "No\n";
query(mat, "orf", len)? cout << "Yes\n" :
cout << "No\n";
return 0;
}
Java
// Java program to answer subsequence queries for
// a given string.
public class Query_Subsequence {
static final int MAX = 10000;
static final int CHAR_SIZE = 26;
// Precompute the position of each character from
// each position of String S
static void precompute(int mat[][], String str, int len)
{
for (int i = 0; i < CHAR_SIZE; ++i)
mat[len][i] = len;
// Computing position of each character from
// each position of String S
for (int i = len-1; i >= 0; --i)
{
for (int j = 0; j < CHAR_SIZE; ++j)
mat[i][j] = mat[i+1][j];
mat[i][str.charAt(i)-'a'] = i;
}
}
// Print "Yes" if T is subsequence of S, else "No"
static boolean query(int mat[][], String str, int len)
{
int pos = 0;
// Traversing the string T
for (int i = 0; i < str.length(); ++i)
{
// If next position is greater than
// length of S set flag to false.
if (mat[pos][str.charAt(i) - 'a'] >= len)
return false;
// Setting position of next character
else
pos = mat[pos][str.charAt(i) - 'a'] + 1;
}
return true;
}
// Driven Program
public static void main(String args[])
{
String S= "geeksforgeeks";
int len = S.length();
int[][] mat = new int[MAX][CHAR_SIZE];
precompute(mat, S, len);
String get = query(mat, "gg", len)? "Yes" :"No";
System.out.println(get);
get = query(mat, "gro", len)? "Yes" :"No";
System.out.println(get);
get = query(mat, "gfg", len)? "Yes" :"No";
System.out.println(get);
get = query(mat, "orf", len)? "Yes" :"No";
System.out.println(get);
}
}
// This code is contributed by Sumit Ghosh
Python3
# Python3 program to answer
# subsequence queries for
# a given string.
MAX = 10000
CHAR_SIZE = 26
# Precompute the position of
# each character from
# each position of String S
def precompute(mat, str, Len):
for i in range(CHAR_SIZE):
mat[Len][i] = Len
# Computing position of each
# character from each position
# of String S
for i in range(Len - 1, -1, -1):
for j in range(CHAR_SIZE):
mat[i][j] = mat[i + 1][j]
mat[i][ord(str[i]) -
ord('a')] = i
# Print "Yes" if T is
# subsequence of S, else "No"
def query(mat, str, Len):
pos = 0
# Traversing the string T
for i in range(len(str)):
# If next position is greater than
# length of S set flag to false.
if(mat[pos][ord(str[i]) -
ord('a')] >= Len):
return False
# Setting position of next character
else:
pos = mat[pos][ord(str[i]) -
ord('a')] + 1
return True
# Driven code
S = "geeksforgeeks"
Len = len(S)
mat = [[0 for i in range(CHAR_SIZE)]
for j in range(MAX)]
precompute(mat, S, Len)
get = "No"
if(query(mat, "gg", Len)):
get = "Yes"
print(get)
get = "No"
if(query(mat, "gro", Len)):
get = "Yes"
print(get)
get = "No"
if(query(mat, "gfg", Len)):
get = "Yes"
print(get)
get = "No"
if(query(mat, "orf", Len)):
get = "Yes"
print(get)
# This code is contributed by avanitrachhadiya2155
C#
// C# program to answer subsequence
// queries for a given string
using System;
public class Query_Subsequence
{
static int MAX = 10000;
static int CHAR_SIZE = 26;
// Precompute the position of each
// character from each position
// of String S
static void precompute(int [,]mat,
string str,
int len)
{
for (int i = 0; i < CHAR_SIZE; ++i)
mat[len, i] = len;
// Computing position of each
// character from each position
// of String S
for (int i = len - 1; i >= 0; --i)
{
for (int j = 0; j < CHAR_SIZE;
++j)
mat[i, j] = mat[i + 1, j];
mat[i, str[i] - 'a'] = i;
}
}
// Print "Yes" if T is subsequence
// of S, else "No"
static bool query(int [,]mat,
string str,
int len)
{
int pos = 0;
// Traversing the string T
for (int i = 0; i < str.Length; ++i)
{
// If next position is greater than
// length of S set flag to false.
if (mat[pos,str[i] - 'a'] >= len)
return false;
// Setting position of next character
else
pos = mat[pos,str[i] - 'a'] + 1;
}
return true;
}
// Driver Code
public static void Main()
{
string S= "geeksforgeeks";
int len = S.Length;
int[,] mat = new int[MAX,CHAR_SIZE];
precompute(mat, S, len);
string get = query(mat, "gg", len)?
"Yes" :"No";
Console.WriteLine(get);
get = query(mat, "gro", len)?
"Yes" :"No";
Console.WriteLine(get);
get = query(mat, "gfg", len)?
"Yes" :"No";
Console.WriteLine(get);
get = query(mat, "orf", len)?
"Yes" :"No";
Console.WriteLine(get);
}
}
// This code is contributed by vt_m.
Javascript
输出:
Yes
No
Yes
No