Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

นับคู่ของสตริงย่อยพาลินโดรมที่ไม่ทับซ้อนใน C++

เราได้รับอินพุตเป็นสตริง ภารกิจคือหาจำนวนคู่ของสตริงย่อยพาลินโดรมที่ไม่ทับซ้อนกันของสตริงอินพุต ค่า arr[i][j] เป็น true หากสตริงย่อยเป็นพาลินโดรม มิฉะนั้น false เราจะนำการจัดหมู่ออกจากสตริงและตรวจสอบว่าคู่ต่างๆ ตรงตามเกณฑ์หรือไม่

ทำความเข้าใจด้วยตัวอย่าง

Input: ABC

Output: จำนวนคู่ของสตริงย่อยพาลินโดรมที่ไม่ทับซ้อนคือ 3

คำอธิบาย: คู่ที่เป็นไปได้คือ (A)(B)(C), (A)(BC), (AB)(C), (ABC)

Input: ABCD

Output: จำนวนคู่ของสตริงย่อยพาลินโดรมที่ไม่ทับซ้อนคือ 8

คำอธิบาย: คู่ที่เป็นไปได้คือ (A)(B)(C)(D), (A)(B)(CD), (A)(BC)(D), (A)(BCD), (AB)(C)(D), (AB)(CD), (ABC)(D), (ABCD)

แนวทางที่ใช้ในโปรแกรมด้านล่าง

  • รับสตริงเป็นอินพุตและส่งให้ฟังก์ชัน pair_count(text) ประมวลผลต่อ
  • สร้างอาร์เรย์ boolean 2D ขนาด 100 arr[][] และเติมแบบ bottom-up แปลงสตริงอินพุตเป็นอาร์เรย์ตัวอักษร
  • คำนวณอาร์เรย์โดยตรวจสอบ arr[i+1][j-1] หากเป็น true และ str[i] == str[j] ตั้ง arr[i][j] = true มิฉะนั้น false
  • หลังจากนั้น เริ่มต้น start[] และ end[] โดย start[i] เก็บจำนวนพาลินโดรมทางซ้ายของดัชนี i (รวม i) และ end[i] เก็บจำนวนทางขวาของดัชนี i (รวม i)
  • วนลูปจาก 0 ถึง str.length()-1 และคำนวณผลลัพธ์โดยบวก start[i] * end[i+1]

ตัวอย่าง

import java.io.*; 
import java.util.*; 
class tutorialPoint { 
    static int SIZE = 100; 
    static int pair_count(String str) { 
        boolean arr[][] = new boolean[SIZE][SIZE]; 
        char[] ch = str.toCharArray(); 
        for (int i = 0; i < ch.length; i++) { 
            for (int j = 0; j < ch.length; j++) { 
                arr[i][j] = false; 
            } 
        } 
        for (int j = 1; j <= ch.length; j++) { 
            for (int i = 0; i <= ch.length - j; i++) { 
                if (j <= 2) { 
                    if (ch[i] == ch[i + j - 1]) { 
                        arr[i][i + j - 1] = true; 
                    } 
                } else if (ch[i] == ch[i + j - 1]) { 
                    arr[i][i + j - 1] = arr[i + 1][i + j - 2]; 
                } 
            } 
        } 
        int start[] = new int[str.length()]; 
        int end[] = new int[str.length()]; 
        start[0] = 1; 
        for (int i = 1; i < str.length(); i++) { 
            for (int j = 0; j <= i; j++) { 
                if (arr[j][i] == true) { 
                    start[i]++; 
                } 
            } 
        } 
        end[str.length() - 1] = 1; 
        for (int i = str.length() - 2; i >= 0; i--) { 
            end[i] = end[i + 1]; 
            for (int j = str.length() - 1; j >= i; j--) { 
                if (arr[i][j] == true) { 
                    end[i]++; 
                } 
            } 
        } 
        int result = 0; 
        for (int i = 0; i < str.length() - 1; i++) { 
            result = result + start[i] * end[i + 1]; 
        } 
        return result; 
    } 
    public static void main(String[] args) { 
        Scanner scan = new Scanner(System.in); 
        String text = scan.next(); 
        System.out.println("Count pairs of non-overlapping palindromic sub-strings is\t" + pair_count(text)); 
    } 
}

หากเรารันโค้ดข้างต้น จะได้ผลลัพธ์ดังนี้

ผลลัพธ์

Count pairs of non-overlapping palindromic sub-strings is 8