查询重复字符串中的字符

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

null
Input : X = "geeksforgeeks", Q = 3.Query 1: 0 8Query 2: 8 13Query 3: 6 15Output :YesYesNoString 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<bits/stdc++.h>
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

<?php
// Queries for same characters
// in a repeated string
// Print whether index i and j
// have same element or not.
function query( $s , $i , $j )
{
$n = strlen ( $s );
// 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 ]))
echo "Yes" ;
else
echo "No" ;
}
// Driver Code
$X = "geeksforgeeks" ;
query( $X , 0, 8);
query( $X , 8, 13);
query( $X , 6, 15);
// This code is contributed by nitin mittal.
?>


Javascript

<script>
// Javascript Program to Queries for
// same characters in a
// repeated string
// Print whether index i and j
// have same element or not
function query(s, i, j)
{
let 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])
document.write( "Yes" + "</br>" );
else
document.write( "No" + "</br>" );
}
let X = "geeksforgeeks" ;
query(X, 0, 8);
query(X, 8, 13);
query(X, 6, 15);
</script>


输出:

YesYesNo

本文由 阿努伊·乔汉 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享