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.mzph.cn/news/725497.shtml

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

相关文章

DataLoader

import torchvision from torch.utils.data import DataLoader from torch.utils.tensorboard import SummaryWriter# 准备的测试数据集 数据放在了CIFAR10文件夹下test_data torchvision.datasets.CIFAR10("./CIFAR10",trainFalse, transformtorchvision.transfor…

Qt入门(一)Qt概述

Qt是什么&#xff1f; Qt是一个跨平台应用开发框架。 Qt既包括了一系列的Qt库&#xff0c;还包括诸多配套的开发工具如QtCreater&#xff0c;GUI Designer。Qt本身是由C开发的&#xff0c;但是也提供了其他编程语言的接口。 Qt的定位以及同类 学一种技术&#xff0c;最重要的是…

PDF控件Spire.PDF for .NET【安全】演示:加密 PDF 文档

加密PDF是人们常用的保护PDF的方法。无论对于公司还是个人&#xff0c;使用PDF加密来设置一些限制都是必不可少的。为了使PDF文档可供未经授权的用户阅读但无法修改&#xff0c;加密的PDF文档需要两个密码&#xff1a;所有者密码和用户密码。本节将特别介绍一种通过 Spire.PDF …

从mysql 数据库表导入数据到elasticSearch的几种方式

从MySQL数据库导入数据到Elasticsearch有几种方式&#xff0c;主要包括以下几种&#xff1a; 1. 使用Logstash&#xff1a; Logstash是一个开源的数据收集引擎&#xff0c;可以用来从不同的数据源导入数据到Elasticsearch。它具有强大的数据处理能力和插件生态系统&…

ChatGPT在地学、GIS、气象、农业、生态、环境等领域中的应用

以ChatGPT、LLaMA、Gemini、DALLE、Midjourney、Stable Diffusion、星火大模型、文心一言、千问为代表AI大语言模型带来了新一波人工智能浪潮&#xff0c;可以面向科研选题、思维导图、数据清洗、统计分析、高级编程、代码调试、算法学习、论文检索、写作、翻译、润色、文献辅助…

scanf从缓冲区读值、检查缓冲区与读取失败

先看一段代码&#xff1a;代码① #include <Windows.h> int main() { int n 0; while (scanf( "%d", &n) ! EOF) // 如果输入a死循环 { printf( "b\n"); //getchar(); Sleep(1000); } return 0; } 此程序的输出结果是 输出…

透明多级分流系统(用户端缓存和负载均衡)

部件考虑 有些设备位于客户端或者网络边缘&#xff0c;能够迅速响应用户请求&#xff0c;避免给cpu和数据库带来压力&#xff0c;比如&#xff0c;本地缓存&#xff0c;内容分发网络&#xff0c;反向代理等。 有些设备处理能力能够线性扩展&#xff0c;易于伸缩&#xff0c;应…

探索程序员职业迷宫:选择适合自己的职业赛道

作为现代社会中备受青睐的职业之一&#xff0c;程序员的职业赛道就如同一座错综复杂的迷宫&#xff0c;充满了挑战与机遇。在这个迷宫中&#xff0c;有着前端的绚丽花园、后端的神秘洞穴以及数据科学的深邃密室&#xff0c;每一条路径都蕴藏着无限可能和发展空间。而如何选择适…

代码随想录Day22 | Leetcode39 组合总和、Leetcode40 数组总和II | Leetcode131 分割回文串

上题 39. 组合总和 - 力扣&#xff08;LeetCode&#xff09; 40. 组合总和 II - 力扣&#xff08;LeetCode&#xff09; 131. 分割回文串 - 力扣&#xff08;LeetCode&#xff09; 第一题 给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target &#xff0c;找…

ChatGPT聊天机器人数据隐私和安全问题

ChatGPT是否安全使用&#xff1f; 是的&#xff0c;ChatGPT是安全的&#xff0c;因为它无法对你或你的计算机造成任何直接损害。由于网页浏览器和智能手机操作系统都使用了沙箱技术&#xff0c;因此ChatGPT无法访问你设备的其余部分。换句话说&#xff0c;当你使用ChatGPT应用程…

node.js中path.join() 和 path.resolve()

《Node.js》path.resolve与path.join的区别与作用_js path.resolve-CSDN博客

Sentinel 面试题及答案整理,最新面试题

Sentinel的流量控制规则有哪些&#xff0c;各自的作用是什么&#xff1f; Sentinel的流量控制规则主要包括以下几种&#xff1a; 1、QPS&#xff08;每秒查询量&#xff09;限流&#xff1a; 限制资源每秒的请求次数&#xff0c;适用于控制高频访问。 2、线程数限流&#xf…

【异常处理】Mybatis报错 source is null for getProperty(null, “length“)

发现问题 <select id"listArticle" resultType"top.ambtwill.blog.dao.pojo.Article">select * from ms_article<where>11<if test"categoryId ! null">and category_id#{categoryId}</if><if test"tagId ! nul…

MacOS安装反编译工具JD-GUI 版本需要1.8+

Java Decompiler http://java-decompiler.github.io/ 将下载下来的 jd-gui-osx-1.6.6.tar 解压&#xff0c;然后将 JD-GUI.app 文件拷贝到 Applications 应用程序目录里面 1.显示包内容 2.找到Contents/MacOS/universalJavaApplicationStub.sh 3.修改sh文件 内容修改为下面…

微服务之商城系统

一、商城系统建立之前的一些配置 1、nacos Nacos是一个功能丰富的开源平台&#xff0c;用于配置管理、服务发现和注册、健康检查等&#xff0c;帮助构建和管理分布式系统。 在linux上安装nacos容器的命令&#xff1a; docker run --name nacos-standalone -e MODEstandalone …

07 - 镜像管理之:镜像优化

1 为什么要做镜像优化? 1&#xff09;随着我们对docker镜像的持续使用&#xff0c;在此过程中如果不加以注意并且优化&#xff0c;镜像的体积会越来越大&#xff0c;很多时候我们在使用docker部署应用时&#xff0c;会发现镜像的体积可能都会在1G以上。 2&#xff09;镜像体积…

08 视图

视图 视图概念 视图是存储的查询语句,当调用的时候,产生结果集,视图充当的是虚拟表的角色。其实视图可以理解为一个表或多个表中导出来的表&#xff0c;作用和真实表一样&#xff0c;包含一系列带有行和列的数据 视图中&#xff0c;用户可以使用SELECT语句查询数据&#xff0…

Fabric V2.5 通用溯源系统——应用前端部分设计及简易二次开发

本节对Fabric V2.5 通用溯源系统的前端部分做一个简单的介绍。包括目录结构、文件作用简述、用户注册登录实现、农产品信息上链溯源实现的介绍。同时提供了简易二次开发的教程(面向需要在短时间内二次开发),将本项目修改为商品溯源项目,仅修改前端部分。本节内容需要订阅《…

Python报错ModuleNotFoundError: No module named ‘numpy‘

原因&#xff1a;缺少“numpy” 进入python安装路径&#xff0c;script路径内 在路径下启动终端 01.更新numpy python -m pip install --upgrade pip 02.安装 pip install numpy 03.运行python python 04.导入包 from numpy import * 问题已解决。

凌鲨客户端架构

客户端架构 客户端使用了tauri作为主框架&#xff0c;通过rust和内置应用(sidecar)为前端界面提供额外能力。 内置应用(sidecar) 应用 相关项目 说明 devc 开发环境容器工具 gitspy 本地git仓库管理工具 grpcutil grpc调用工具 mongo 通讯协议 mongo协议转发工具 …