在C ++中查询重复字符串中的字符
在这个问题中,我们给了一个字符串str和Q查询,它们由两个值a和b组成。我们的任务是创建一个程序来解决C++中重复字符串中字符的查询。
问题描述
为了解决每个查询,我们需要检查索引a和b处的字符是否相同,并相应地返回值。
让我们举个例子来了解这个问题,
输入:str=“tutorialspoint”
Q=2
查询={{0,2},{4,7}}
输出:重复
不重复
说明
对于查询1,索引0处的字符是t,索引2处的字符是t。两者都是相同的字符。
对于查询1,索引4处的字符是r,索引7处的字符是l。两者都不是相同的字符。
解决方法
为了解决这个问题,我们将简单地检查索引a和b处元素的等效性。如果任何索引大于字符串的长度,则be将找到(index%len)的值,以获取字符的索引。与用户新索引进行比较。
示例
#include <iostream>
#include <string>
using namespace std;
bool isrepeated(string str, int len, int a, int b){
if(a > len)
a %= len;
if(b > len)
b %= len;
if(str[a] == str[b])
return true;
else
return false;
}
int main(){
string str = "tutorialspoint";
int len = str.length();
int Q = 3;
int query[Q][2] = {{0, 2}, {3, 32}, {5, 18}};
for(int i = 0; i < Q; i++){
if(isrepeated(str, len, query[i][0], query[i][1]))
cout<<"Character is repeated in both the index values"<<endl;
else
cout<<"Character is not repeated in both the index values"<<endl;
}
return 0;
}输出结果
Character is repeated in both the index values Character is not repeated in both the index values Character is not repeated in both the index values
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语