用 C++,找出信号到达字符串中所有位置所用的时间
在本教程中,我们将讨论一个程序,找出信号到达字符串中所有位置所用的时间
为此,我们会提供一个包含 ‘x’ 和 ‘o’ 的字符串。一个信号源自 ‘x’,并向两个方向传播,在单位时间内将一个 'o' 值更改为 'x'。我们的任务是计算将整个字符串转换为 'x' 需要的总时间。
示例
#include <bits/stdc++.h>
using namespace std;
//calculating the total required time
int findMaximumDuration(string s, int n) {
int right = 0, left = 0;
int count = 0, maximumLength = INT_MIN;
s = s + '1';
for (int i = 0; i <= n; i++) {
if (s[i] == 'o')
count++;
else {
if (count > maximumLength) {
right = 0;
left = 0;
if (s[i] == 'x')
right = 1;
if (((i - count) > 0) && (s[i - count - 1] == 'x'))
left = 1;
count = ceil((double)count / (right + left));
maximumLength = max(maximumLength, count);
}
count = 0;
}
}
return maximumLength;
}
int main() {
string str = "xooxoooxxoooxoooxooxooox";
int length = str.size();
cout << findMaximumDuration(str, length);
return 0;
}输出
2
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP