在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 送给学长毕业祝福语简短