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 算法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
    }
}

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

VS code下载与使用方法(包含远程调试)

Visual Studio Code(简称 VSCode)是由微软开发的一款免费、开源、跨平台的现代化轻量级代码编辑器。它具有丰富的功能和强大的扩展性,适用于多种编程语言和开发环境。以下是 VSCode 的一些主要特点和功能: 跨平台支持: 可在 Windows、macOS 和 Linux 等多种操作系…

基于ACM32 MCU的两轮车充电桩方案,打造高效安全的电池管理

随着城市化进程的加快、人们生活水平的提高和节能环保理念的普及&#xff0c;越来越多的人选择了电动车作为代步工具&#xff0c;而两轮电动车的出行半径较短&#xff0c;需要频繁充电&#xff0c;因此在城市中设置两轮车充电桩就非常有必要了。城市中的充电桩不仅能解决两轮车…

Flink:Temporal Table 的两种实现方式 Temporal Table DDL 和 Temporal Table Function

博主历时三年精心创作的《大数据平台架构与原型实现&#xff1a;数据中台建设实战》一书现已由知名IT图书品牌电子工业出版社博文视点出版发行&#xff0c;点击《重磅推荐&#xff1a;建大数据平台太难了&#xff01;给我发个工程原型吧&#xff01;》了解图书详情&#xff0c;…

近地面无人机植被定量遥感与生理参数反演技术应用

李老师&#xff08;副教授&#xff09;&#xff0c;长期从事无人机近地面植被遥感&#xff0c;植被生理参数&#xff0c;多角度遥感&#xff0c;RGB/多光谱/高光谱数据处理&#xff0c;LiDAR点云处理等领域研究工作&#xff0c;具有资深的技术底蕴和专业背景。 专题一、近十年…

java 获取项目内的资源/配置文件

【getResourceAsStream】是java中用于获取项目内资源的常用方法&#xff0c;能够返回一个数据流&#xff0c;从而允许我们读取指定路径下的资源文件。这个方法可以用来读取各种类型的资源文件&#xff0c;包括但不限于文本文件、图像文件、配置文件等。 要使用getResourceAsStr…

InfluxDB SHOW SERIES语句按照什么顺序返回?

本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。 本作品 (李兆龙 博文, 由 李兆龙 创作)&#xff0c;由 李兆龙 确认&#xff0c;转载请注明版权。 文章目录 引言样例SHOW SERIES比较原理结论结束语 引言 influxdb的计算引擎为了做到自底而上的…

【Web安全靶场】upload-labs-master 1-21

upload-labs-master 其他靶场见专栏… 文章目录 upload-labs-masterPass-01-js前端校验Pass-02-MIME校验Pass-03-其他后缀绕过黑名单Pass-04-.hatccess绕过Pass-05-点空格点代码逻辑绕过Pass-06-大小写绕过Pass-07-空格绕过Pass-08-点号绕过Pass-09-::$DATA绕过Pass-10-点空格…

三、代码结构(不定时更新)

一、装饰器 Entry&#xff1a;标记当前组件是入口组件 Component&#xff1a;标记自定义组件 State&#xff1a;标记该变量是状态变量&#xff0c;值变化时会触发UI刷新 二、自定义组件 // 可复用的UI单元 struct Index {} 三、UI描述 // 其内部以声明式方式描述UI结构 bu…

fatal: unable to access ‘***‘: OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 0解决方案

本文收录于《AI绘画从入门到精通》专栏&#xff0c;专栏总目录&#xff1a;点这里。 大家好&#xff0c;我是水滴~~ 本文主要介绍在从 GitHub 上克隆 stable-diffusion-webui 项目时出现的 fatal: unable to access https://github.com/AUTOMATIC1111/stable-diffusion-webui.…

【STM32】HAL库 CubeMX教程---通用定时器 定时

STM32常用型号的TIM时钟频率 1. STM32F103系列&#xff1a; 所有 TIM 的时钟频率都是72MHz&#xff1b;F103C8不带基本定时器&#xff0c;F103RC及以上才带基本定时器。 2、STM32F407系列&#xff1a; TIM1、8、9、10、11的时钟频率是168MHz&#xff1b;其它TIM的时钟频率是…

【PHP】PHP通过串口与硬件通讯,向硬件设备发送数据并接收硬件返回的数据

一、前言 之前写过两篇PHP实现与硬件串口交互的文章&#xff0c;一篇是【PHP】PHP实现与硬件串口交互&#xff0c;接收硬件发送的实时数据&#xff08;上&#xff09;_php串口通信-CSDN博客&#xff0c;另一篇是【PHP】PHP实现与硬件串口交互&#xff0c;向硬件设备发送指令数…

阿里云2核4G服务器支持人数并发测试,2核4G主机测评

阿里云2核4G服务器多少钱一年&#xff1f;2核4G配置1个月多少钱&#xff1f;2核4G服务器30元3个月、轻量应用服务器2核4G4M带宽165元一年、企业用户2核4G5M带宽199元一年。可以在阿里云CLUB中心查看 aliyun.club 当前最新2核4G服务器精准报价、优惠券和活动信息。 阿里云官方2…

C++ LRU缓存

题目&#xff1a; //构建双向链表的节点结构&#xff08;要有两个构造函数&#xff09; struct Node{int key, val;Node* pre;Node* next;Node():key(0), val(0), pre(nullptr), next(nullptr) {}Node(int _key, int _val): key(_key), val(_val), pre(nullptr), next(nullpt…

基础小白快速入门web前端开发技术------>web概述

Web概述 我们在编程的学习中&#xff0c;随着学习的深入&#xff0c;我们会理解到WEB这个东西&#xff0c;那么 web究竟是个啥&#xff0c;到底该咋用&#xff1f; web&#xff0c;是网站的英文意思&#xff0c;又被称作“下一代Web3.0&#xff0c;互联网”&#xff0c;是在We…

简洁实用的wordpress外贸网站模板

坚果蜜饯wordpress跨境电商模板 木瓜干、菠萝干、夏威夷果、芒果干、椰片、巴旦木等wordpress跨境电商模板。 https://www.jianzhanpress.com/?p3944 珠宝手饰wordpress外贸网站模板 金银手饰、珍珠手饰、翡翠手饰、钻石手饰、玉石珠宝手饰wordpress外贸网站模板。 https:…

docker无法运行问题

场景如下&#xff1a; 执行运行docker命令出现如下错误&#xff1a;systemctl start docker 出现该问题的原因&#xff1a;是因为我们配置的镜像加速器用不了了 去修改我们的镜像加速器&#xff0c; 去到配置镜像加速器的目录 cd /etc/docker 修改镜像加速器 vim daemon.j…

记一次 .NET某设备监控自动化系统 CPU爆高分析

一&#xff1a;背景 1. 讲故事 先说一下题外话&#xff0c;一个监控别人系统运行状态的程序&#xff0c;结果自己出问题了&#xff0c;有时候想一想还是挺讽刺的&#xff0c;哈哈&#xff0c;开个玩笑&#xff0c;我们回到正题&#xff0c;前些天有位朋友找到我&#xff0c;说…

二叉树进阶leetcode

606. 根据二叉树创建字符串 要点&#xff1a;前序遍历&#xff0c;当左子树为空时&#xff0c;右结点有数字时要给左边加括号 class Solution { public:string tree2str(TreeNode* root) {string s;//创建一个字符串if(rootnullptr){return s;}sto_string(root->val);//保存…

LLM | GPT-NEOX论文详解

GPT-NEOX使用旋转位置编码。模型权重使用float16表示。最大序列长度为2048。 论文题目&#xff1a;2022.04.14_GPT-NeoX-20B: An Open-Source Autoregressive Language Model 论文地址&#xff1a;2204.06745.pdf (arxiv.org) 论文代码&#xff1a;EleutherAI/gpt-neox: An imp…

go语言基础 -- 文件操作

基础的文件操作方法 go里面的文件操作封装在os包里面的File结构体中&#xff0c;要用的时候最好去查下官方文档&#xff0c;这里介绍下基本的文件操作。 打开关闭文件 import("os" ) func main() {// Open返回*File指针&#xff0c;后续的操作都通过*File对象操作…