有人可以为我解释一下或者用 C++ 编写一个程序吗?接到任务但不知道怎么做。
问题:给你一组仅包含
a
和 b
的字符串,你的程序应该能够检查每个字符串中是否有相同数量的 a
和 b
或不是。
例如如果程序得到
true
,它将响应 {ab, aabb, aaabbbb, bbbaaa}
,并在得到 false
时说
{aab, bbba, aaabbbb}
使用堆栈解决它
#include <iostream>
#include <string>
#include <stack>
#include <algorithm>
using namespace std;
int count1 = 0;
int count2 = 0;
bool isInLanguageL (string w);
int main()
{
string input;
cout << "Input any string; ";
getline(cin,input);
if (input.length() % 2 != 0)
cout <<"Pattern entered does not match the language ";
else
isInLanguageL(input);
return 0;
}
bool isInLanguageL (string w)
{
stack<string> word1, word2;
string a, b;
for (unsigned i = 0; i < w.length()/2; i++)
{
a = w.at(i);
word1.push(a);
}
reverse(w.begin(), w.end());
for (unsigned i = 0; i < w.length()/2; i++)
{
b = w.at(i);
word2.push(b);
}
while(!word1.empty() && !word2.empty())
{
word1.pop();
count1 = count1++;
word2.pop();
count2 = count2++;
}
if(count1 == count2)
return true;
else
return false;
}
本方案使用了栈,请参考代码中的注释。如果有任何疑问可以评论。
代码:
#include <iostream>
#include <stack>
#include <string>
using namespace std;
void checkString(string s) {
if (s.size() % 2 != 0) {
cout << "Doesn't satisfy the conditon\n";
return;
}
stack<char> st;
int n = s.size();
for (int i = 0; i < n; ++i) {
/*
case - 1 : If the stack is empty you can directly push the current character into the stack
case - 2 : If there are elements present in the stack, then if the current character is equal to the top character on the stack then we can push the current character
beacuse we didn't find any new character to match them.
*/
if (st.empty() || (st.top() == s[i])) {
st.push(s[i]);
}
/*
case-3 : If the stack is not emtpy and current character is different from the top character on the stack then we found a match like a-b (OR) b-a, so then we will
remove the top element from the stack and move to next character of the string
*/
else if (st.top() != s[i]) {
st.pop();
}
}
/*
case - 1 : After iterating through all the characters in the string, if we find the stack is emtpy then we can say all characters are not matched
case - 2 : If stack is emtpy, then that means all the characters are matched.
*/
(st.empty()) ? (cout << "Yes, satisfies the conditon\n") : (cout << "Doesn't satisfy the conditon\n");
}
int main() {
string s = "";
cin >> s;
checkString(s);
return 0;
}
您的解决方案有许多错误,您可能应该使用调试器来解决这些错误。这是参考。
此解决方案没有按照您的要求使用堆栈,但您可以编写此函数来使用算法来解决您的问题:
namespace rs = std::ranges;
bool all_equal_as_and_bs(auto const & strings)
{
return rs::all_of(strings, [](auto const & string)
{
return rs::count(string, 'a') == rs::count(string, 'b');
});
}
并像这样使用它:
all_equal_as_and_bs(std::vector<std::string>{"ab", "aabb", "aaabbb", "bbbaaa"}); // true
all_equal_as_and_bs(std::vector<std::string>{"aab", "bba", "aaabbbb", "bbbaaa"}); // false
public boolean ab(String s){
String[] st= s.split("");
Stack d2 = new Stack;
Stack d1 = new Stack;
int c1 = 0,c2 = 0;
for(int i=0;i<st.length/2;i++){
d1.push(1);
}
for(int i=0;i<st.length/2;i++){
d2.push(2);
}
while(!d1.isEmpty()){
d1.pop();
c1++;
}
while(!d1.isEmpty()){
d2.pop();
c2++;
}
if(c1==c2)
return true;
else
return false;
}