C++ 代码检查数组是否可以由相等不等序列形成


假设我们有一个长度为 n 的字符串 S。考虑有 n 个数字,它们按圆形排列。我们不知道这些数字的值,但如果 S[i] = 'E',则表示第 i 个和第 (i+1) 个数字相同,但如果是 'N',则它们不同。从 S 中,我们必须检查是否我们可以重新创建序列。

因此,如果输入类似 S = "ENNEENE",则输出将为 True,因为我们可以分配 [15,15,4,20,20,20,15] 等值。

步骤

为了解决这个问题,我们将遵循以下步骤 -

if S has single 'N', then: return false return true

示例

让我们查看以下实现以获得更好的理解 -

Open Compiler
#include<bits/stdc++.h> using namespace std; bool solve(string S){ if (count(S.begin(), S.end(), 'N') == 1) return false; return true; } int main(){ string S = "ENNEENE"; cout << solve(S) << endl; }

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

输入

"ENNEENE"

输出

1

更新日期:2022-03-11

131 次浏览

开启你的 事业

完成课程,获得认证

开始
广告