我正在解决一个问题,指出:要更改每个“?”如果字符串中不包含'a',则不包含连续的'a',否则用'b'代替,例如。 a?b将是abb而不是aab,因为这里有2个a是连续的。
我的问题是我= 3,根据我的代码,我的字符串应该用'b'覆盖,它正进入所需的块中,但是字符串没有被b覆盖,但是在所有其他情况下应该写上“ a”的地方。帮我解决这些问题。
您可以从此处参考问题说明以更好地理解我的问题:https://www.hackerearth.com/practice/algorithms/greedy/basics-of-greedy-algorithms/practice-problems/algorithm/exploring-ruins/
#include <iostream>
using namespace std;
int main() {
string str;
cin >> str;
int n = str.size();
for(int i = 0; i < str.size(); i++) {
if(str[i] == '?') {
if(i == 0) {
if(str[i + 1] == 'a')
str[i] = 'b';
else
str[i] = 'a';
cout << "I am in if" << endl;
} else if(i == n - 1) {
if(str[i - 1] == 'a')
str[i] == 'b';
else
str[i] == 'a';
cout << "I am in if of else if " << endl;
} else {
if(str[i + 1] == 'a' || str[i - 1] == 'a') {
str[i] == 'b';
cout << "I am in if of else " << endl;
} else {
str[i] = 'a';
cout << "I am in else of else " << endl;
}
}
cout << str[i] << endl;
} else
continue;
}
cout << str << endl;
return 0;
}
给出字符串:?ba ?? b 期望的输出:ababab 我的输出:aba?ab
答案 0 :(得分:0)
如果您使用函数来解决此问题,对您来说会容易得多。
{ some_array: target_order.sort_by { |h| h[:new_position] }.map { |h| h.slice(:id) } }
#=> {:some_array=>[{:id=>"foo3"}, {:id=>"foo2"}, {:id=>"foo1"}, {:id=>"foo0"}]}