C#,回文分割问题(Palindrome Partitioning Problem)算法与源代码

1 回文串

“回文串”是一个正读和反读都一样的字符串,初始化标志flag=true,比如“level”或者“noon”等等就是回文串。

2 回文分割问题

给定一个字符串,如果该字符串的每个子字符串都是回文的,那么该字符串的分区就是回文分区。
例如,“aba | b | bbabb | a | b | aba”是“abababababa”的回文分区。
确定给定字符串的回文分区所需的最少切割。
例如,“ababababababa”至少需要3次切割。
 这三个分段是“a | babbab | b | ababa”。
如果字符串是回文,则至少需要0个分段。
如果一个长度为n的字符串包含所有不同的字符,则至少需要n-1个分段。

3 源程序

using System;
using System.Collections;
using System.Collections.Generic;

namespace Legalsoft.Truffer.Algorithm
{
    public static partial class Algorithm_Gallery
    {
        #region 算法1
        private static bool PPP_IsPalindrome(string String, int i, int j)
        {
            while (i < j)
            {
                if (String[i] != String[j])
                {
                    return false;
                }
                i++;
                j--;
            }
            return true;
        }

        public static int Min_Palindrome_Partion(string str, int i, int j)
        {
            if (i >= j || PPP_IsPalindrome(str, i, j))
            {
                return 0;
            }
            int ans = Int32.MaxValue, count;
            for (int k = i; k < j; k++)
            {
                count = Min_Palindrome_Partion(str, i, k) + Min_Palindrome_Partion(str, k + 1, j) + 1;

                ans = Math.Min(ans, count);
            }
            return ans;
        }
        #endregion

        #region 算法2
        public static int Min_Palindrome_Partion(string str)
        {
            int n = str.Length;
            int[,] C = new int[n, n];
            bool[,] P = new bool[n, n];

            int i, j, k, L;
            for (i = 0; i < n; i++)
            {
                P[i, i] = true;
                C[i, i] = 0;
            }

            for (L = 2; L <= n; L++)
            {
                for (i = 0; i < n - L + 1; i++)
                {
                    j = i + L - 1;
                    if (L == 2)
                    {
                        P[i, j] = (str[i] == str[j]);
                    }
                    else
                    {
                        P[i, j] = (str[i] == str[j]) && P[i + 1, j - 1];
                    }
                    if (P[i, j] == true)
                    {
                        C[i, j] = 0;
                    }
                    else
                    {
                        C[i, j] = int.MaxValue;
                        for (k = i; k <= j - 1; k++)
                        {
                            C[i, j] = Math.Min(C[i, j], C[i, k] + C[k + 1, j] + 1);
                        }
                    }
                }
            }
            return C[0, n - 1];
        }
        #endregion

        #region 算法3
        public static int Min_Cutting(string a)
        {
            int[] cut = new int[a.Length];
            bool[,] palindrome = new bool[a.Length, a.Length];
            for (int i = 0; i < a.Length; i++)
            {
                int minCut = i;
                for (int j = 0; j <= i; j++)
                {
                    if (a[i] == a[j] && (i - j < 2 || palindrome[j + 1, i - 1]))
                    {
                        palindrome[j, i] = true;
                        minCut = Math.Min(minCut, j == 0 ? 0 : (cut[j - 1] + 1));
                    }
                }
                cut[i] = minCut;
            }
            return cut[a.Length - 1];
        }
        #endregion

        #region 算法4
        public static int Min_Palindrome_Partion_Second(string str)
        {
            int n = str.Length;
            int[] C = new int[n];
            bool[,] P = new bool[n, n];

            int i, j, L;
            for (i = 0; i < n; i++)
            {
                P[i, i] = true;
            }

            for (L = 2; L <= n; L++)
            {
                for (i = 0; i < n - L + 1; i++)
                {
                    j = i + L - 1;
                    if (L == 2)
                    {
                        P[i, j] = (str[i] == str[j]);
                    }
                    else
                    {
                        P[i, j] = (str[i] == str[j]) && P[i + 1, j - 1];
                    }
                }
            }

            for (i = 0; i < n; i++)
            {
                if (P[0, i] == true)
                {
                    C[i] = 0;
                }
                else
                {
                    C[i] = int.MaxValue;
                    for (j = 0; j < i; j++)
                    {
                        if (P[j + 1, i] == true && 1 + C[j] < C[i])
                        {
                            C[i] = 1 + C[j];
                        }
                    }
                }
            }
            return C[n - 1];
        }
        #endregion

        #region 算法5
        private static string PPP_Hashcode(int a, int b)
        {
            return a.ToString() + "" + b.ToString();
        }

        public static int Min_Palindrome_Partiion_Memoizatoin(string input, int i, int j, Hashtable memo)
        {
            if (i > j)
            {
                return 0;
            }
            string ij = PPP_Hashcode(i, j);
            if (memo.ContainsKey(ij))
            {
                return (int)memo[ij];
            }

            if (i == j)
            {
                memo.Add(ij, 0);
                return 0;
            }
            if (PPP_IsPalindrome(input, i, j))
            {
                memo.Add(ij, 0);
                return 0;
            }
            int minimum = Int32.MaxValue;

            for (int k = i; k < j; k++)
            {
                int left_min = Int32.MaxValue;
                int right_min = Int32.MaxValue;
                string left = PPP_Hashcode(i, k);
                string right = PPP_Hashcode(k + 1, j);

                if (memo.ContainsKey(left))
                {
                    left_min = (int)memo[left];
                }
                if (memo.ContainsKey(right))
                {
                    right_min = (int)memo[right];
                }

                if (left_min == Int32.MaxValue)
                {
                    left_min = Min_Palindrome_Partiion_Memoizatoin(input, i, k, memo);
                }
                if (right_min == Int32.MaxValue)
                {
                    right_min = Min_Palindrome_Partiion_Memoizatoin(input, k + 1, j, memo);
                }
                minimum = Math.Min(minimum, left_min + 1 + right_min);
            }

            memo.Add(ij, minimum);

            return (int)memo[ij];
        }
        #endregion
    }
}
 

POWER BY TRUFFER.CN 

4 源代码

using System;
using System.Collections;
using System.Collections.Generic;namespace Legalsoft.Truffer.Algorithm
{public static partial class Algorithm_Gallery{#region 算法1private static bool PPP_IsPalindrome(string String, int i, int j){while (i < j){if (String[i] != String[j]){return false;}i++;j--;}return true;}public static int Min_Palindrome_Partion(string str, int i, int j){if (i >= j || PPP_IsPalindrome(str, i, j)){return 0;}int ans = Int32.MaxValue, count;for (int k = i; k < j; k++){count = Min_Palindrome_Partion(str, i, k) + Min_Palindrome_Partion(str, k + 1, j) + 1;ans = Math.Min(ans, count);}return ans;}#endregion#region 算法2public static int Min_Palindrome_Partion(string str){int n = str.Length;int[,] C = new int[n, n];bool[,] P = new bool[n, n];int i, j, k, L;for (i = 0; i < n; i++){P[i, i] = true;C[i, i] = 0;}for (L = 2; L <= n; L++){for (i = 0; i < n - L + 1; i++){j = i + L - 1;if (L == 2){P[i, j] = (str[i] == str[j]);}else{P[i, j] = (str[i] == str[j]) && P[i + 1, j - 1];}if (P[i, j] == true){C[i, j] = 0;}else{C[i, j] = int.MaxValue;for (k = i; k <= j - 1; k++){C[i, j] = Math.Min(C[i, j], C[i, k] + C[k + 1, j] + 1);}}}}return C[0, n - 1];}#endregion#region 算法3public static int Min_Cutting(string a){int[] cut = new int[a.Length];bool[,] palindrome = new bool[a.Length, a.Length];for (int i = 0; i < a.Length; i++){int minCut = i;for (int j = 0; j <= i; j++){if (a[i] == a[j] && (i - j < 2 || palindrome[j + 1, i - 1])){palindrome[j, i] = true;minCut = Math.Min(minCut, j == 0 ? 0 : (cut[j - 1] + 1));}}cut[i] = minCut;}return cut[a.Length - 1];}#endregion#region 算法4public static int Min_Palindrome_Partion_Second(string str){int n = str.Length;int[] C = new int[n];bool[,] P = new bool[n, n];int i, j, L;for (i = 0; i < n; i++){P[i, i] = true;}for (L = 2; L <= n; L++){for (i = 0; i < n - L + 1; i++){j = i + L - 1;if (L == 2){P[i, j] = (str[i] == str[j]);}else{P[i, j] = (str[i] == str[j]) && P[i + 1, j - 1];}}}for (i = 0; i < n; i++){if (P[0, i] == true){C[i] = 0;}else{C[i] = int.MaxValue;for (j = 0; j < i; j++){if (P[j + 1, i] == true && 1 + C[j] < C[i]){C[i] = 1 + C[j];}}}}return C[n - 1];}#endregion#region 算法5private static string PPP_Hashcode(int a, int b){return a.ToString() + "" + b.ToString();}public static int Min_Palindrome_Partiion_Memoizatoin(string input, int i, int j, Hashtable memo){if (i > j){return 0;}string ij = PPP_Hashcode(i, j);if (memo.ContainsKey(ij)){return (int)memo[ij];}if (i == j){memo.Add(ij, 0);return 0;}if (PPP_IsPalindrome(input, i, j)){memo.Add(ij, 0);return 0;}int minimum = Int32.MaxValue;for (int k = i; k < j; k++){int left_min = Int32.MaxValue;int right_min = Int32.MaxValue;string left = PPP_Hashcode(i, k);string right = PPP_Hashcode(k + 1, j);if (memo.ContainsKey(left)){left_min = (int)memo[left];}if (memo.ContainsKey(right)){right_min = (int)memo[right];}if (left_min == Int32.MaxValue){left_min = Min_Palindrome_Partiion_Memoizatoin(input, i, k, memo);}if (right_min == Int32.MaxValue){right_min = Min_Palindrome_Partiion_Memoizatoin(input, k + 1, j, memo);}minimum = Math.Min(minimum, left_min + 1 + right_min);}memo.Add(ij, minimum);return (int)memo[ij];}#endregion}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/517591.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Java】关于ZooKeeper的原理以及一致性问题,协议和算法和ZooKeeper的理论知识和应用 场景

1. 目录 目录 1. 目录 2. 什么是ZooKeeper 3. 一致性问题 4. 一致性协议和算法 4.1. 2PC&#xff08;两阶段提交&#xff09; 4.2. 3PC&#xff08;三阶段提交&#xff09; 4.3. Paxos 算法 4.3.1. prepare 阶段 4.3.2. accept 阶段 4.3.3. paxos 算法的死循环…

【数据结构高阶】并查集

目录 一、什么是并查集 二、并查集的原理 三、并查集的作用 四、并查集的代码实现 一、什么是并查集 在一些应用问题中&#xff0c;需要将n个不同的元素划分成一些不相交的集合。开始时&#xff0c;每个元素自成一个 单元素集合&#xff0c;然后按一定的规律将归于同一组元…

如何远程访问电脑文件?

远程访问电脑文件是当今数字化时代中十分常见且实用的技术。它允许我们从任何地方的计算机或移动设备访问和操作我们的电脑中的文件。无论是远程工作、远程学习、远程协作还是方便地获得自己计算机上的重要文件&#xff0c;远程访问电脑文件都为我们提供了巨大的便利。 在远程访…

像SpringBoot一样使用Flask - 2.静态资源访问及模版

一、安装并导入 render_template 功能&#xff1a;渲染/加载模板&#xff0c;一般是html页面 参数&#xff1a;函数的第一个参数是模板的文件名&#xff0c;必填&#xff0c;后面的参数都是键值对&#xff0c;表示模板中变量对应的值&#xff0c;非必填 (不填界面也不会展示成变…

大路灯哪个牌子好?5大热卖大路灯汇总,年度首选

网上总能搜到很多和大路灯相关的话题&#xff0c;像“大路灯伤眼”、“大路灯是否有用”等很是常见。由此可见&#xff0c;大路灯虽然荣升最受欢迎的照明电器&#xff0c;但依旧备受争议。从根源上说&#xff0c;是不专业大路灯抢占市场&#xff0c;它们的基础品质得不到保障&a…

字符串标记高亮脚本

源码 #!/bin/bash # usage: # echo hhh|mark str [font_color] [background_color] # font_color and background_color is optional, default is black&whiterp_str$1 f_color30 b_color47if [ "${f_color}a" "a" ]; thenf_color30 fiif [ "${…

node的安装与介绍

安装 下载地址 node官网首页就会有两个安装选择&#xff0c;会根据当前电脑的系统自动显示对应的安装包&#xff0c;一个长期维护版&#xff08;LTS&#xff09;,一个是尝鲜版&#xff0c;记住选择LTS版本 安装指定版本下载截图 安装过程截图&#xff08;非常简单&#xff…

常用的电生理肌电信号数据合集 (EMG)

目录 Ninapro CapgMyo-DBa CSL-HDEMG EMGLAB Sleep Heart Health Study Ninapro Ninapro 是一个公开的多模式数据库&#xff0c;旨在促进人类、机器人和假肢手的机器学习研究。 10 个 Ninapro 数据集总共包括来自完整受试者和经桡动脉截肢者的 180 多个数据采集&#xff…

【MySQL】索引优化与关联查询优化

数据库调优的几个维度&#xff1a; 索引失效&#xff0c;没有充分用到索引——索引建立关联查询太多JOIN——SQL优化服务器调优以及各个参数设置——调整my.cnf数据过多——分库分表 SQL查询优化的几种方式&#xff1a; 物理查询优化&#xff1a;通过索引以及表连接方式进行…

Anaconda prompt运行打开jupyter notebook 指令出错

一、打不开jupyter notebook网页 报错如下&#xff1a; Traceback (most recent call last): File “D:\anaconda3\lib\site-packages\notebook\traittypes.py”, line 235, in _resolve_classes klass self._resolve_string(klass) File “C:\Users\DELL\AppData\Roaming\Py…

021—pandas 书单整理将同一种书整理在一起

前言 在办公自动化场景下&#xff0c;最常见的需求就是信息的整理&#xff0c;pandas 最擅长复杂数据逻辑的处理&#xff0c;能够让整理工作更加高效&#xff0c;同时不容易出错。今天的案例是将一个平铺的书单按品类进行整理&#xff0c;合并为一行。 需求: 将书按书名进行合…

盲人的智慧出行:与APP共赴无障碍之旅

在黑暗中&#xff0c;我从未感到孤单。作为一名盲人&#xff0c;我选择与一款盲人辅助软件蝙蝠避障一同踏上盲人智慧出行之旅&#xff0c;探寻生活中的无限可能。 这款软件拥有先进的障碍物识别技术&#xff0c;其基于先进的激光雷达探测&#xff0c;通过智能语音和触觉反馈&am…