📌  相关文章
📜  查询重复字符中的字符串

📅  最后修改于: 2022-05-13 01:57:08.215000             🧑  作者: Mango

查询重复字符中的字符串

给定一个字符串X 。通过多次重复字符串X 形成字符串S,即将字符串X 与其自身多次附加。有形式 i 和 j 的Q查询。任务是如果索引 i 处的元素与 S 中索引 j 处的元素相同则打印“是”,否则为每个查询打印“否”。
例子 :

Input : X = "geeksforgeeks", Q = 3.
Query 1: 0 8
Query 2: 8 13
Query 3: 6 15

Output :
Yes
Yes
No

String S will be "geeksforgeeksgeeksforgeeks....".
For Query 1, index 0 and index 8 have same element i.e 'g'.
For Query 2, index 8 and index 13 have same element i.e 'g'.
For Query 3, index 6 = 'o' and index 15 = 'e' which are not same.

设字符串X 的长度为 n。观察索引 0、n、2n、3n、…处的元素。是一样的。类似地,对于索引 i,位置 i、n+i、2n+i、3n+i、….. 包含相同的元素。
因此,对于每个查询,查找 (i%n) 和 (j%n) 以及字符串X 是否相同。
以下是上述想法的实现:

C++
// Queries for same characters in a repeated
// string
#include
using namespace std;
 
// Print whether index i and j have same
// element or not.
void query(char s[], int i, int j)
{
    int n = strlen(s);
 
    // Finding relative position of index i,j.
    i %= n;
    j %= n;
 
    // Checking is element are same at index i, j.
    (s[i]==s[j])? (cout << "Yes" << endl):
                  (cout << "No" << endl);
}
 
// Driven Program
int main()
{
    char X[] = "geeksforgeeks";
 
    query(X, 0, 8);
    query(X, 8, 13);
    query(X, 6, 15);
 
    return 0;
}


Java
// Java Program to Queries for
// same characters in a
// repeated string
import java.io.*;
 
public class GFG{
     
// Print whether index i and j
// have same element or not
static void query(String s, int i,
                  int j)
{
    int n = s.length();
 
    // Finding relative position
    // of index i,j
    i %= n;
    j %= n;
 
    // Checking is element are same
    // at index i, j
    if(s.charAt(i) == s.charAt(j))
    System.out.println("Yes");
    else
    System.out.println("No");
}
 
    // Driver Code
    static public void main (String[] args)
    {
    String X = "geeksforgeeks";
 
    query(X, 0, 8);
    query(X, 8, 13);
    query(X, 6, 15);
 
    }
}
 
// This code is contributed by vt_m.


Python3
# Queries for same characters in a repeated
# string
 
# Print whether index i and j have same
# element or not.
def query(s, i, j):
    n = len(s)
 
    # Finding relative position of index i,j.
    i %= n
    j %= n
 
    # Checking is element are same at index i, j.
    print("Yes") if s[i] == s[j] else print("No")
 
# Driver code
if __name__ == "__main__":
    X = "geeksforgeeks"
    query(X, 0, 8)
    query(X, 8, 13)
    query(X, 6, 15)
 
# This code is contributed by
# sanjeev2552


C#
// C# Program to Queries for
// same characters in a
// repeated string
using System;
 
public class GFG{
     
// Print whether index i and j
// have same element or not
static void query(string s, int i,
                  int j)
{
    int n = s.Length;
 
    // Finding relative position
    // of index i,j.
    i %= n;
    j %= n;
 
    // Checking is element are
    // same at index i, j
    if(s[i] == s[j])
    Console.WriteLine("Yes");
    else
    Console.WriteLine("No");
}
 
    // Driver Code
    static public void Main ()
    {
    string X = "geeksforgeeks";
 
    query(X, 0, 8);
    query(X, 8, 13);
    query(X, 6, 15);
 
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出 :

Yes
Yes
No