📜  给定琴弦的双音子字符串

📅  最后修改于: 2021-04-29 11:41:15             🧑  作者: Mango

在给定字符串str的情况下,任务是对给定字符串的所有双音子字符串进行计数。

例子:

方法:想法是生成给定字符串的所有可能的子字符串,并检查每个子字符串是否为Bitonic。如果子字符串是Bitonic,则增加答案的计数。最后,打印所有双子位子阵列的计数。

下面是上述方法的实现:

C++
// C+++ program for the above approach
#include 
using namespace std;
 
// Function to find all the bitonic
// sub strings
void subString(char str[], int n)
{
    // Pick starting point
    int c = 0;
 
    // Iterate till length of the string
    for (int len = 1; len <= n; len++) {
 
        // Pick ending point for string
        for (int i = 0; i <= n - len; i++) {
 
            // Substring from i to j
            // is obtained
            int j = i + len - 1;
            char temp = str[i], f = 0;
 
            // Substrings of length 1
            if (j == i) {
 
                // Increase count
                c++;
                continue;
            }
 
            int k = i + 1;
 
            // For increasing sequence
            while (temp < str[k] && k <= j) {
                temp = str[k];
                k++;
                f = 2;
            }
 
            // Check for strictly increasing
            if (k > j) {
 
                // Increase count
                c++;
                f = 2;
            }
 
            // Check for decreasing sequence
            while (temp > str[k]
                   && k <= j
                   && f != 2) {
                k++;
                f = 0;
            }
 
            if (k > j && f != 2) {
 
                // Increase count
                c++;
                f = 0;
            }
        }
    }
 
    // Print the result
    cout << c << endl;
}
 
// Driver Code
int main()
{
    // Given string
    char str[] = "bade";
 
    // Function Call
    subString(str, strlen(str));
    return 0;
}


Java
// Java+ program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find all the bitonic
// sub Strings
static void subString(char str[], int n)
{
     
    // Pick starting point
    int c = 0;
 
    // Iterate till length of the String
    for(int len = 1; len <= n; len++)
    {
         
        // Pick ending point for String
        for(int i = 0; i <= n - len; i++)
        {
             
            // SubString from i to j
            // is obtained
            int j = i + len - 1;
            char temp = str[i], f = 0;
 
            // SubStrings of length 1
            if (j == i)
            {
                 
                // Increase count
                c++;
                continue;
            }
 
            int k = i + 1;
 
            // For increasing sequence
            while (k < n && temp < str[k])
            {
                temp = str[k];
                k++;
                f = 2;
            }
 
            // Check for strictly increasing
            if (k > j)
            {
 
                // Increase count
                c++;
                f = 2;
            }
 
            // Check for decreasing sequence
            while (k < n && temp > str[k] &&
                   f != 2)
            {
                k++;
                f = 0;
            }
 
            if (k > j && f != 2)
            {
 
                // Increase count
                c++;
                f = 0;
            }
        }
    }
 
    // Print the result
    System.out.print(c + "\n");
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given String
    char str[] = "bade".toCharArray();
 
    // Function call
    subString(str, str.length);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Function to find all the bitonic
# sub strings
def subString(str, n):
 
    # Pick starting po
    c = 0;
 
    # Iterate till length of the string
    for len in range(1, n + 1):
 
        # Pick ending pofor string
        for i in range(0, n - len + 1):
 
            # Substring from i to j
            # is obtained
            j = i + len - 1;
            temp = str[i]
            f = 0;
 
            # Substrings of length 1
            if (j == i):
 
                # Increase count
                c += 1
                continue;
            k = i + 1;
 
            # For increasing sequence
            while (k <= j and temp < str[k]):
                temp = str[k];
                k += 1;
                f = 2;
             
            # Check for strictly increasing
            if (k > j):
 
                # Increase count
                c += 1;
                f = 2;
             
            # Check for decreasing sequence
            while (k <= j and temp > str[k] and
                   f != 2):
                k += 1;
                f = 0;
            if (k > j and f != 2):
 
                # Increase count
                c += 1;
                f = 0;
                 
    # Print the result
    print(c)
 
# Driver code
 
# Given string
str = "bade";
 
# Function Call
subString(str, len(str))
     
# This code is contributed by grand_master


C#
// C# program for the above approach
using System;
class GFG{
 
// Function to find all the bitonic
// sub Strings
static void subString(char []str, int n)
{
     
    // Pick starting point
    int c = 0;
 
    // Iterate till length of the String
    for(int len = 1; len <= n; len++)
    {
         
        // Pick ending point for String
        for(int i = 0; i <= n - len; i++)
        {
             
            // SubString from i to j
            // is obtained
            int j = i + len - 1;
            char temp = str[i], f = (char)0;
 
            // SubStrings of length 1
            if (j == i)
            {
                 
                // Increase count
                c++;
                continue;
            }
 
            int k = i + 1;
 
            // For increasing sequence
            while (k < n && temp < str[k])
            {
                temp = str[k];
                k++;
                f = (char)2;
            }
 
            // Check for strictly increasing
            if (k > j)
            {
 
                // Increase count
                c++;
                f = (char)2;
            }
 
            // Check for decreasing sequence
            while (k < n && temp > str[k] &&
                f != 2)
            {
                k++;
                f = (char)0;
            }
 
            if (k > j && f != 2)
            {
 
                // Increase count
                c++;
                f = (char)0;
            }
        }
    }
 
    // Print the result
    Console.Write(c + "\n");
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given String
    char []str = "bade".ToCharArray();
 
    // Function call
    subString(str, str.Length);
}
}
 
// This code is contributed by 29AjayKumar


输出:
8



时间复杂度: O(N 2 )
辅助空间: O(1)