通过根据相应的字符串值将位置插入数组来生成序列
给定一个长度为N的字符串S。该字符串仅由字母'F'和'B'组成。任务是生成一个执行某些操作的序列,例如:
- 考虑一个仅由一个 0 组成的整数序列A ,即 A = (0)。
- 现在,对于字符串(1 到 N)的每个索引(i),如果S[i]是“F”,则将i添加到序列A中i-1的最前面(即左侧)
- 否则,如果S[i]是'B',则将i添加到i-1序列A的紧靠后面(即右侧)。
- 打印结果序列A 。
例子:
Input: N = 5, S = “FBBFB”
Output: 1 2 4 5 3 0
Explanation: Initially, A = {0}.
S[1] is ‘F’ , sequence becomes {1, 0}
S[2] is ‘B’ , sequence becomes {1, 2, 0}
S[3] is ‘B’ , sequence becomes {1, 2, 3, 0}
S[4] is ‘F’ , sequence becomes {1, 2, 4, 3, 0}
S[5] is ‘B’ , sequence becomes {1, 2, 4, 5, 3, 0}
Input : N = 6 , S = “BBBBBB”
Output : 0 1 2 3 4 5 6
方法:解决问题的想法是基于出队的概念。
As at each iteration, it is possible that insertion of i may occur from any end of (i-1), therefore deque can be used as in dequeue insertion is possible from any end.
按照解决方案的步骤:
- 该方法的观察结果是,在反转所有操作后问题变得更加简单。
- 现在问题修改为,给定的序列仅包含 (N),并且在从字符串末尾开始的每次迭代之后,
- 如果S[i]是 'F',推 i在i-1的右边,
- 如果S[i]是 'B',则将i推到 dequeue 中i-1的左侧。
- 从前端开始按顺序返回dequeue的元素。
下面是上述方法的实现。
C++
// C++ program for above approach
#include
using namespace std;
// Function to find sequence
// from given string
// according to given rule
void findSequence(int N, string S)
{
// Creating a deque
deque v;
// Inserting N (size of string) into deque
v.push_back(N);
// Iterating string from behind and
// pushing the indices into the deque
for (int i = N - 1; i >= 0; i--) {
// If letter at current index is 'F',
// push i to the right of i-1
if (S[i] == 'F') {
v.push_back(i);
}
// If letter at current index is 'B',
// push i to the left of i-1
else {
v.push_front(i);
}
}
// Printing resultant sequence
for (int i = 0; i <= N; i++)
cout << v[i] << " ";
}
// Driver Code
int main()
{
int N = 5;
string S = "FBBFB";
// Printing the sequence
findSequence(N, S);
return 0;
}
Java
// JAVA program for above approach
import java.util.*;
class GFG
{
// Function to find sequence
// from given string
// according to given rule
public static void findSequence(int N, String S)
{
// Creating a deque
Deque v = new ArrayDeque();
// Inserting N (size of string) into deque
v.addLast(N);
// Iterating string from behind and
// pushing the indices into the deque
for (int i = N - 1; i >= 0; i--) {
// If letter at current index is 'F',
// push i to the right of i-1
if (S.charAt(i) == 'F') {
v.addLast(i);
}
// If letter at current index is 'B',
// push i to the left of i-1
else {
v.addFirst(i);
}
}
// Printing resultant sequence
for (Iterator itr = v.iterator(); itr.hasNext();) {
System.out.print(itr.next() + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int N = 5;
String S = "FBBFB";
// Printing the sequence
findSequence(N, S);
}
}
// This code is contributed by Taranpreet
Python3
# Python program for above approach
from collections import deque
# Function to find sequence
# from given string
# according to given rule
def findSequence(N, S):
# Creating a deque
v = deque()
# Inserting N (size of string) into deque
v.append(N)
# Iterating string from behind and
# pushing the indices into the deque
i = N - 1
while(i >= 0):
# If letter at current index is 'F',
# push i to the right of i-1
if (S[i] == 'F'):
v.append(i)
# If letter at current index is 'B',
# push i to the left of i-1
else:
v.appendleft(i)
i -= 1
# Printing resultant sequence
print(*v)
# Driver Code
N = 5
S = "FBBFB"
# Printing the sequence
findSequence(N, S)
# This code is contributed by Samim Hossain Mondal.
C#
// C# program for above approach
using System;
using System.Collections.Generic;
public class GFG
{
// Function to find sequence
// from given string
// according to given rule
public static void findSequence(int N, String S)
{
// Creating a deque
List v = new List();
// Inserting N (size of string) into deque
v.Add(N);
// Iterating string from behind and
// pushing the indices into the deque
for (int i = N - 1; i >= 0; i--) {
// If letter at current index is 'F',
// push i to the right of i-1
if (S[i] == 'F') {
v.Add(i);
}
// If letter at current index is 'B',
// push i to the left of i-1
else {
v.Insert(0,i);
}
}
// Printing resultant sequence
foreach (int itr in v) {
Console.Write(itr + " ");
}
}
// Driver Code
public static void Main(String[] args)
{
int N = 5;
String S = "FBBFB";
// Printing the sequence
findSequence(N, S);
}
}
// This code is contributed by 29AjayKumar
Javascript
输出
1 2 4 5 3 0
时间复杂度: 在)
辅助空间: 在)