C#,哈夫曼编码(Huffman Code)压缩(Compress )与解压缩(Decompress)算法与源代码

news2024/10/23 19:18:41

 David A. Huffman

1 哈夫曼编码简史(Huffman code)

1951年,哈夫曼和他在MIT信息论的同学需要选择是完成学期报告还是期末考试。导师Robert M. Fano给他们的学期报告的题目是,寻找最有效的二进制编码。由于无法证明哪个已有编码是最有效的,哈夫曼放弃对已有编码的研究,转向新的探索,最终发现了基于有序频率二叉树编码的想法,并很快证明了这个方法是最有效的。由于这个算法,学生终于青出于蓝,超过了他那曾经和信息论创立者香农共同研究过类似编码的导师。哈夫曼使用自底向上的方法构建二叉树,避免了次优算法Shannon-Fano编码的最大弊端──自顶向下构建树。
1952年,David A. Huffman在麻省理工攻读博士时发表了《一种构建极小多余编码的方法》,一文,它一般就叫做Huffman编码。A Method for the Construction of Minimum-Redundancy Codesicon-default.png?t=N7T8https://www.ias.ac.in/article/fulltext/reso/011/02/0091-0099
Huffman在1952年根据香农(Shannon)在1948年和范若(Fano)在1949年阐述的这种编码思想提出了一种不定长编码的方法,也称霍夫曼(Huffman)编码。霍夫曼编码的基本方法是先对图像数据扫描一遍,计算出各种像素出现的概率,按概率的大小指定不同长度的唯一码字,由此得到一张该图像的霍夫曼码表。编码后的图像数据记录的是每个像素的码字,而码字与实际像素值的对应关系记录在码表中。

2 赫夫曼编码

赫夫曼编码是可变字长编码(VLC)的一种。 Huffman于1952年提出一种编码方法,该方法完全依据字符出现概率来构造异字头的平均长 度最短的码字,有时称之为最佳编码,一般就称Huffman编码。下面引证一个定理,该定理保证了按字符出现概率分配码长,可使平均码长最短。

3 源程序

using System;
using System.IO;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

namespace Legalsoft.Truffer.Algorithm
{
    /// <summary>
    /// 哈夫曼编码的压缩与解压缩
    /// </summary>
    public class HuffmanCompressor
    {
        private HuffmanNode oRootHuffmanNode { get; set; } = null;
        private List<HuffmanNode> oValueHuffmanNodes { get; set; } = null;

        private List<HuffmanNode> BuildBinaryTree(string Value)
        {
            List<HuffmanNode> oHuffmanNodes = GetInitialNodeList();

            Value.ToList().ForEach(m => oHuffmanNodes[m].Weight++);

            oHuffmanNodes = oHuffmanNodes
                .Where(m => (m.Weight > 0))
                .OrderBy(m => (m.Weight))
                .ThenBy(m => (m.Value))
                .ToList();

            oHuffmanNodes = UpdateNodeParents(oHuffmanNodes);

            oRootHuffmanNode = oHuffmanNodes[0];
            oHuffmanNodes.Clear();

            SortNodes(oRootHuffmanNode, oHuffmanNodes);

            return oHuffmanNodes;
        }

        public void Compress(string FileName)
        {
            FileInfo oFileInfo = new FileInfo(FileName);

            if (oFileInfo.Exists == true)
            {
                string sFileContents = "";

                using (StreamReader oStreamReader = new StreamReader(File.OpenRead(FileName)))
                {
                    sFileContents = oStreamReader.ReadToEnd();
                }

                List<HuffmanNode> oHuffmanNodes = BuildBinaryTree(sFileContents);

                oValueHuffmanNodes = oHuffmanNodes
                    .Where(m => (m.Value.HasValue == true))
                    .OrderBy(m => (m.BinaryWord))
                    .ToList();

                Dictionary<char, string> oCharToBinaryWordDictionary = new Dictionary<char, string>();
                foreach (HuffmanNode oHuffmanNode in oValueHuffmanNodes)
                {
                    oCharToBinaryWordDictionary.Add(oHuffmanNode.Value.Value, oHuffmanNode.BinaryWord);
                }

                StringBuilder oStringBuilder = new StringBuilder();
                List<byte> oByteList = new List<byte>();
                for (int i = 0; i < sFileContents.Length; i++)
                {
                    string sWord = "";

                    oStringBuilder.Append(oCharToBinaryWordDictionary[sFileContents[i]]);

                    while (oStringBuilder.Length >= 8)
                    {
                        sWord = oStringBuilder.ToString().Substring(0, 8);

                        oStringBuilder.Remove(0, sWord.Length);
                    }

                    if (String.IsNullOrEmpty(sWord) == false)
                    {
                        oByteList.Add(Convert.ToByte(sWord, 2));
                    }
                }

                if (oStringBuilder.Length > 0)
                {
                    string sWord = oStringBuilder.ToString();

                    if (String.IsNullOrEmpty(sWord) == false)
                    {
                        oByteList.Add(Convert.ToByte(sWord, 2));
                    }
                }

                string sCompressedFileName = Path.Combine(oFileInfo.Directory.FullName, String.Format("{0}.compressed", oFileInfo.Name.Replace(oFileInfo.Extension, "")));
                if (File.Exists(sCompressedFileName) == true)
                {
                    File.Delete(sCompressedFileName);
                }

                using (FileStream oFileStream = File.OpenWrite(sCompressedFileName))
                {
                    oFileStream.Write(oByteList.ToArray(), 0, oByteList.Count);
                }
            }
        }

        public void Decompress(string FileName)
        {
            FileInfo oFileInfo = new FileInfo(FileName);

            if (oFileInfo.Exists == true)
            {
                string sCompressedFileName = String.Format("{0}.compressed", oFileInfo.FullName.Replace(oFileInfo.Extension, ""));

                byte[] oBuffer = null;
                using (FileStream oFileStream = File.OpenRead(sCompressedFileName))
                {
                    oBuffer = new byte[oFileStream.Length];
                    oFileStream.Read(oBuffer, 0, oBuffer.Length);
                }

                HuffmanNode oZeroHuffmanNode = oRootHuffmanNode;
                while (oZeroHuffmanNode.Left != null)
                {
                    oZeroHuffmanNode = oZeroHuffmanNode.Left;
                }

                HuffmanNode oCurrentHuffmanNode = null;
                StringBuilder oStringBuilder = new StringBuilder();

                for (int i = 0; i < oBuffer.Length; i++)
                {
                    string sBinaryWord = "";
                    byte oByte = oBuffer[i];

                    if (oByte == 0)
                    {
                        sBinaryWord = oZeroHuffmanNode.BinaryWord;
                    }
                    else
                    {
                        sBinaryWord = Convert.ToString(oByte, 2);
                    }

                    if ((sBinaryWord.Length < 8) && (i < (oBuffer.Length - 1)))
                    {
                        StringBuilder oBinaryStringBuilder = new StringBuilder(sBinaryWord);
                        while (oBinaryStringBuilder.Length < 8)
                        {
                            oBinaryStringBuilder.Insert(0, "0");
                        }

                        sBinaryWord = oBinaryStringBuilder.ToString();
                    }

                    for (int j = 0; j < sBinaryWord.Length; j++)
                    {
                        char cValue = sBinaryWord[j];

                        if (oCurrentHuffmanNode == null)
                        {
                            oCurrentHuffmanNode = oRootHuffmanNode;
                        }

                        if (cValue == '0')
                        {
                            oCurrentHuffmanNode = oCurrentHuffmanNode.Left;
                        }
                        else
                        {
                            oCurrentHuffmanNode = oCurrentHuffmanNode.Right;
                        }

                        if ((oCurrentHuffmanNode.Left == null) && (oCurrentHuffmanNode.Right == null))
                        {
                            oStringBuilder.Append(oCurrentHuffmanNode.Value.Value);
                            oCurrentHuffmanNode = null;
                        }
                    }
                }

                string sUncompressedFileName = Path.Combine(oFileInfo.Directory.FullName, String.Format("{0}.uncompressed", oFileInfo.Name.Replace(oFileInfo.Extension, "")));

                if (File.Exists(sUncompressedFileName) == true)
                {
                    File.Delete(sUncompressedFileName);
                }

                using (StreamWriter oStreamWriter = new StreamWriter(File.OpenWrite(sUncompressedFileName)))
                {
                    oStreamWriter.Write(oStringBuilder.ToString());
                }
            }
        }

        private static List<HuffmanNode> GetInitialNodeList()
        {
            List<HuffmanNode> oGetInitialNodeList = new List<HuffmanNode>();

            for (int i = Char.MinValue; i < Char.MaxValue; i++)
            {
                oGetInitialNodeList.Add(new HuffmanNode((char)(i)));
            }

            return oGetInitialNodeList;
        }

        private static void SortNodes(HuffmanNode Node, List<HuffmanNode> Nodes)
        {
            if (Nodes.Contains(Node) == false)
            {
                Nodes.Add(Node);
            }

            if (Node.Left != null)
            {
                SortNodes(Node.Left, Nodes);
            }

            if (Node.Right != null)
            {
                SortNodes(Node.Right, Nodes);
            }
        }

        private static List<HuffmanNode> UpdateNodeParents(List<HuffmanNode> Nodes)
        {
            while (Nodes.Count > 1)
            {
                int iOperations = (Nodes.Count / 2);
                for (int iOperation = 0, i = 0, j = 1; iOperation < iOperations; iOperation++, i += 2, j += 2)
                {
                    if (j < Nodes.Count)
                    {
                        HuffmanNode oParentHuffmanNode = new HuffmanNode(Nodes[i], Nodes[j]);
                        Nodes.Add(oParentHuffmanNode);

                        Nodes[i] = null;
                        Nodes[j] = null;
                    }
                }

                Nodes = Nodes
                    .Where(m => (m != null))
                    .OrderBy(m => (m.Weight))
                    .ToList();
            }

            return Nodes;
        }
    }

    public class HuffmanNode
    {
        private string sBinaryWord { get; set; } = "";
        private bool bIsLeftNode { get; set; } = false;
        private bool bIsRightNode { get; set; } = false;
        private HuffmanNode oLeft { get; set; } = null;
        private HuffmanNode oParent { get; set; } = null;
        private HuffmanNode oRight { get; set; } = null;
        private char? cValue { get; set; } = ' ';
        private int iWeight { get; set; } = 0;

        public HuffmanNode()
        {
        }

        public HuffmanNode(char Value)
        {
            cValue = Value;
        }

        public HuffmanNode(HuffmanNode Left, HuffmanNode Right)
        {
            oLeft = Left;
            oLeft.oParent = this;
            oLeft.bIsLeftNode = true;

            oRight = Right;
            oRight.oParent = this;
            oRight.bIsRightNode = true;

            iWeight = (oLeft.Weight + oRight.Weight);
        }

        public string BinaryWord
        {
            get
            {
                string sReturnValue = "";

                if (String.IsNullOrEmpty(sBinaryWord) == true)
                {
                    StringBuilder oStringBuilder = new StringBuilder();

                    HuffmanNode oHuffmanNode = this;

                    while (oHuffmanNode != null)
                    {
                        if (oHuffmanNode.bIsLeftNode == true)
                        {
                            oStringBuilder.Insert(0, "0");
                        }

                        if (oHuffmanNode.bIsRightNode == true)
                        {
                            oStringBuilder.Insert(0, "1");
                        }

                        oHuffmanNode = oHuffmanNode.oParent;
                    }

                    sReturnValue = oStringBuilder.ToString();
                    sBinaryWord = sReturnValue;
                }
                else
                {
                    sReturnValue = sBinaryWord;
                }

                return sReturnValue;
            }
        }

        public HuffmanNode Left
        {
            get
            {
                return oLeft;
            }
        }

        public HuffmanNode Parent
        {
            get
            {
                return oParent;
            }
        }

        public HuffmanNode Right
        {
            get
            {
                return oRight;
            }
        }

        public char? Value
        {
            get
            {
                return cValue;
            }
        }

        public int Weight
        {
            get
            {
                return iWeight;
            }
            set
            {
                iWeight = value;
            }
        }

        public static int BinaryStringToInt32(string Value)
        {
            int iBinaryStringToInt32 = 0;

            for (int i = (Value.Length - 1), j = 0; i >= 0; i--, j++)
            {
                iBinaryStringToInt32 += ((Value[j] == '0' ? 0 : 1) * (int)(Math.Pow(2, i)));
            }

            return iBinaryStringToInt32;
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            if (cValue.HasValue == true)
            {
                sb.AppendFormat("'{0}' ({1}) - {2} ({3})", cValue.Value, iWeight, BinaryWord, BinaryStringToInt32(BinaryWord));
            }
            else
            {
                if ((oLeft != null) && (oRight != null))
                {
                    if ((oLeft.Value.HasValue == true) && (oRight.Value.HasValue == true))
                    {
                        sb.AppendFormat("{0} + {1} ({2})", oLeft.Value, oRight.Value, iWeight);
                    }
                    else
                    {
                        sb.AppendFormat("{0}, {1} - ({2})", oLeft, oRight, iWeight);
                    }
                }
                else
                {
                    sb.Append(iWeight);
                }
            }

            return sb.ToString();
        }
    }
}
 

4 源代码

using System;
using System.IO;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

namespace Legalsoft.Truffer.Algorithm
{
    /// <summary>
    /// 哈夫曼编码的压缩与解压缩
    /// </summary>
    public class HuffmanCompressor
    {
        private HuffmanNode oRootHuffmanNode { get; set; } = null;
        private List<HuffmanNode> oValueHuffmanNodes { get; set; } = null;

        private List<HuffmanNode> BuildBinaryTree(string Value)
        {
            List<HuffmanNode> oHuffmanNodes = GetInitialNodeList();

            Value.ToList().ForEach(m => oHuffmanNodes[m].Weight++);

            oHuffmanNodes = oHuffmanNodes
                .Where(m => (m.Weight > 0))
                .OrderBy(m => (m.Weight))
                .ThenBy(m => (m.Value))
                .ToList();

            oHuffmanNodes = UpdateNodeParents(oHuffmanNodes);

            oRootHuffmanNode = oHuffmanNodes[0];
            oHuffmanNodes.Clear();

            SortNodes(oRootHuffmanNode, oHuffmanNodes);

            return oHuffmanNodes;
        }

        public void Compress(string FileName)
        {
            FileInfo oFileInfo = new FileInfo(FileName);

            if (oFileInfo.Exists == true)
            {
                string sFileContents = "";

                using (StreamReader oStreamReader = new StreamReader(File.OpenRead(FileName)))
                {
                    sFileContents = oStreamReader.ReadToEnd();
                }

                List<HuffmanNode> oHuffmanNodes = BuildBinaryTree(sFileContents);

                oValueHuffmanNodes = oHuffmanNodes
                    .Where(m => (m.Value.HasValue == true))
                    .OrderBy(m => (m.BinaryWord))
                    .ToList();

                Dictionary<char, string> oCharToBinaryWordDictionary = new Dictionary<char, string>();
                foreach (HuffmanNode oHuffmanNode in oValueHuffmanNodes)
                {
                    oCharToBinaryWordDictionary.Add(oHuffmanNode.Value.Value, oHuffmanNode.BinaryWord);
                }

                StringBuilder oStringBuilder = new StringBuilder();
                List<byte> oByteList = new List<byte>();
                for (int i = 0; i < sFileContents.Length; i++)
                {
                    string sWord = "";

                    oStringBuilder.Append(oCharToBinaryWordDictionary[sFileContents[i]]);

                    while (oStringBuilder.Length >= 8)
                    {
                        sWord = oStringBuilder.ToString().Substring(0, 8);

                        oStringBuilder.Remove(0, sWord.Length);
                    }

                    if (String.IsNullOrEmpty(sWord) == false)
                    {
                        oByteList.Add(Convert.ToByte(sWord, 2));
                    }
                }

                if (oStringBuilder.Length > 0)
                {
                    string sWord = oStringBuilder.ToString();

                    if (String.IsNullOrEmpty(sWord) == false)
                    {
                        oByteList.Add(Convert.ToByte(sWord, 2));
                    }
                }

                string sCompressedFileName = Path.Combine(oFileInfo.Directory.FullName, String.Format("{0}.compressed", oFileInfo.Name.Replace(oFileInfo.Extension, "")));
                if (File.Exists(sCompressedFileName) == true)
                {
                    File.Delete(sCompressedFileName);
                }

                using (FileStream oFileStream = File.OpenWrite(sCompressedFileName))
                {
                    oFileStream.Write(oByteList.ToArray(), 0, oByteList.Count);
                }
            }
        }

        public void Decompress(string FileName)
        {
            FileInfo oFileInfo = new FileInfo(FileName);

            if (oFileInfo.Exists == true)
            {
                string sCompressedFileName = String.Format("{0}.compressed", oFileInfo.FullName.Replace(oFileInfo.Extension, ""));

                byte[] oBuffer = null;
                using (FileStream oFileStream = File.OpenRead(sCompressedFileName))
                {
                    oBuffer = new byte[oFileStream.Length];
                    oFileStream.Read(oBuffer, 0, oBuffer.Length);
                }

                HuffmanNode oZeroHuffmanNode = oRootHuffmanNode;
                while (oZeroHuffmanNode.Left != null)
                {
                    oZeroHuffmanNode = oZeroHuffmanNode.Left;
                }

                HuffmanNode oCurrentHuffmanNode = null;
                StringBuilder oStringBuilder = new StringBuilder();

                for (int i = 0; i < oBuffer.Length; i++)
                {
                    string sBinaryWord = "";
                    byte oByte = oBuffer[i];

                    if (oByte == 0)
                    {
                        sBinaryWord = oZeroHuffmanNode.BinaryWord;
                    }
                    else
                    {
                        sBinaryWord = Convert.ToString(oByte, 2);
                    }

                    if ((sBinaryWord.Length < 8) && (i < (oBuffer.Length - 1)))
                    {
                        StringBuilder oBinaryStringBuilder = new StringBuilder(sBinaryWord);
                        while (oBinaryStringBuilder.Length < 8)
                        {
                            oBinaryStringBuilder.Insert(0, "0");
                        }

                        sBinaryWord = oBinaryStringBuilder.ToString();
                    }

                    for (int j = 0; j < sBinaryWord.Length; j++)
                    {
                        char cValue = sBinaryWord[j];

                        if (oCurrentHuffmanNode == null)
                        {
                            oCurrentHuffmanNode = oRootHuffmanNode;
                        }

                        if (cValue == '0')
                        {
                            oCurrentHuffmanNode = oCurrentHuffmanNode.Left;
                        }
                        else
                        {
                            oCurrentHuffmanNode = oCurrentHuffmanNode.Right;
                        }

                        if ((oCurrentHuffmanNode.Left == null) && (oCurrentHuffmanNode.Right == null))
                        {
                            oStringBuilder.Append(oCurrentHuffmanNode.Value.Value);
                            oCurrentHuffmanNode = null;
                        }
                    }
                }

                string sUncompressedFileName = Path.Combine(oFileInfo.Directory.FullName, String.Format("{0}.uncompressed", oFileInfo.Name.Replace(oFileInfo.Extension, "")));

                if (File.Exists(sUncompressedFileName) == true)
                {
                    File.Delete(sUncompressedFileName);
                }

                using (StreamWriter oStreamWriter = new StreamWriter(File.OpenWrite(sUncompressedFileName)))
                {
                    oStreamWriter.Write(oStringBuilder.ToString());
                }
            }
        }

        private static List<HuffmanNode> GetInitialNodeList()
        {
            List<HuffmanNode> oGetInitialNodeList = new List<HuffmanNode>();

            for (int i = Char.MinValue; i < Char.MaxValue; i++)
            {
                oGetInitialNodeList.Add(new HuffmanNode((char)(i)));
            }

            return oGetInitialNodeList;
        }

        private static void SortNodes(HuffmanNode Node, List<HuffmanNode> Nodes)
        {
            if (Nodes.Contains(Node) == false)
            {
                Nodes.Add(Node);
            }

            if (Node.Left != null)
            {
                SortNodes(Node.Left, Nodes);
            }

            if (Node.Right != null)
            {
                SortNodes(Node.Right, Nodes);
            }
        }

        private static List<HuffmanNode> UpdateNodeParents(List<HuffmanNode> Nodes)
        {
            while (Nodes.Count > 1)
            {
                int iOperations = (Nodes.Count / 2);
                for (int iOperation = 0, i = 0, j = 1; iOperation < iOperations; iOperation++, i += 2, j += 2)
                {
                    if (j < Nodes.Count)
                    {
                        HuffmanNode oParentHuffmanNode = new HuffmanNode(Nodes[i], Nodes[j]);
                        Nodes.Add(oParentHuffmanNode);

                        Nodes[i] = null;
                        Nodes[j] = null;
                    }
                }

                Nodes = Nodes
                    .Where(m => (m != null))
                    .OrderBy(m => (m.Weight))
                    .ToList();
            }

            return Nodes;
        }
    }

    public class HuffmanNode
    {
        private string sBinaryWord { get; set; } = "";
        private bool bIsLeftNode { get; set; } = false;
        private bool bIsRightNode { get; set; } = false;
        private HuffmanNode oLeft { get; set; } = null;
        private HuffmanNode oParent { get; set; } = null;
        private HuffmanNode oRight { get; set; } = null;
        private char? cValue { get; set; } = ' ';
        private int iWeight { get; set; } = 0;

        public HuffmanNode()
        {
        }

        public HuffmanNode(char Value)
        {
            cValue = Value;
        }

        public HuffmanNode(HuffmanNode Left, HuffmanNode Right)
        {
            oLeft = Left;
            oLeft.oParent = this;
            oLeft.bIsLeftNode = true;

            oRight = Right;
            oRight.oParent = this;
            oRight.bIsRightNode = true;

            iWeight = (oLeft.Weight + oRight.Weight);
        }

        public string BinaryWord
        {
            get
            {
                string sReturnValue = "";

                if (String.IsNullOrEmpty(sBinaryWord) == true)
                {
                    StringBuilder oStringBuilder = new StringBuilder();

                    HuffmanNode oHuffmanNode = this;

                    while (oHuffmanNode != null)
                    {
                        if (oHuffmanNode.bIsLeftNode == true)
                        {
                            oStringBuilder.Insert(0, "0");
                        }

                        if (oHuffmanNode.bIsRightNode == true)
                        {
                            oStringBuilder.Insert(0, "1");
                        }

                        oHuffmanNode = oHuffmanNode.oParent;
                    }

                    sReturnValue = oStringBuilder.ToString();
                    sBinaryWord = sReturnValue;
                }
                else
                {
                    sReturnValue = sBinaryWord;
                }

                return sReturnValue;
            }
        }

        public HuffmanNode Left
        {
            get
            {
                return oLeft;
            }
        }

        public HuffmanNode Parent
        {
            get
            {
                return oParent;
            }
        }

        public HuffmanNode Right
        {
            get
            {
                return oRight;
            }
        }

        public char? Value
        {
            get
            {
                return cValue;
            }
        }

        public int Weight
        {
            get
            {
                return iWeight;
            }
            set
            {
                iWeight = value;
            }
        }

        public static int BinaryStringToInt32(string Value)
        {
            int iBinaryStringToInt32 = 0;

            for (int i = (Value.Length - 1), j = 0; i >= 0; i--, j++)
            {
                iBinaryStringToInt32 += ((Value[j] == '0' ? 0 : 1) * (int)(Math.Pow(2, i)));
            }

            return iBinaryStringToInt32;
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            if (cValue.HasValue == true)
            {
                sb.AppendFormat("'{0}' ({1}) - {2} ({3})", cValue.Value, iWeight, BinaryWord, BinaryStringToInt32(BinaryWord));
            }
            else
            {
                if ((oLeft != null) && (oRight != null))
                {
                    if ((oLeft.Value.HasValue == true) && (oRight.Value.HasValue == true))
                    {
                        sb.AppendFormat("{0} + {1} ({2})", oLeft.Value, oRight.Value, iWeight);
                    }
                    else
                    {
                        sb.AppendFormat("{0}, {1} - ({2})", oLeft, oRight, iWeight);
                    }
                }
                else
                {
                    sb.Append(iWeight);
                }
            }

            return sb.ToString();
        }
    }
}

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

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

相关文章

和为K的子数组

题目&#xff1a; 使用前缀和的方法可以解决这个问题&#xff0c;因为我们需要找到和为k的连续子数组的个数。通过计算前缀和&#xff0c;我们可以将问题转化为求解两个前缀和之差等于k的情况。 假设数组的前缀和数组为prefixSum&#xff0c;其中prefixSum[i]表示从数组起始位…

JavaWeb-Maven基础

Maven是专门用于管理和构建Java项目的工具&#xff0c;是 Apache 下的一个纯 Java 开发的开源项目&#xff0c;基于项目对象模型&#xff08;POM&#xff09;概念。先来学习一下Maven基础&#xff0c;等后面学完开发框架后再学Maven高级&#xff0c;这次的内容如下 一、概述 …

主题乐园如何让新客变熟客,让游客变“留客”?

群硕跨越时间结识了一位爱讲故事的父亲&#xff0c;他汇集了一群幻想工程师&#xff0c;打算以故事为基础&#xff0c;建造一个梦幻的主题乐园。 这个乐园后来成为全球游客最多、收入最高的乐园之一&#xff0c;不仅在2023财年创下了近90亿&#xff08;美元&#xff09;的营收…

9款世界级垂直领域软件架构师Visio平替作图工具!

1 LucidChart 一个基于HTML5的在线流程图绘制和协作应用平台&#xff0c;用户可以通过它方便快速的实现流程图表的绘制&#xff0c;同时还可以实现与他人进行实时的流程图绘制和修改功能&#xff0c;对需要群组协作功能的团队来说&#xff0c;这点非常方便。 由于LucidChart是…

机器学习-面经(part8、贝叶斯和其他知识点)

机器学习面经其他系列 机器学习面经系列的其他部分如下所示&#xff1a; 机器学习-面经(part1)-初步说明 机器学习-面经(part2)-交叉验证、超参数优化、评价指标等内容 机器学习-面经(part3)-正则化、特征工程面试问题与解答合集机器学习-面经(part4)-决策树共5000字的面试问…

【CSP试题回顾】201503-3-节日

CSP-201503-3-节日 关键点&#xff1a;格式化输出 在C中&#xff0c;格式化输出通常利用iostream库中的功能&#xff0c;特别是iomanip头文件提供的一系列操作符。这些操作符用于控制输出格式&#xff0c;如宽度、填充、对齐方式等。在你提供的代码中&#xff0c;用于格式化输…

python 输入和输出

在 Python 中&#xff0c;输入和输出是最基本的操作之一。你可以使用内置函数 input() 来获取用户输入&#xff0c;使用 print() 函数来输出信息到控制台。 输入&#xff08;Input&#xff09; input() 函数用于从用户那里获取输入。这个函数会将用户的输入作为字符串返回。 示…

蓝桥杯(3.7)

P1102 A-B 数对 import java.util.Scanner; public class Main {public static void main(String[] args) {Scanner sc new Scanner(System.in);int n sc.nextInt();int c sc.nextInt();int[] res new int[n1];for(int i1;i<n;i)res[i] sc.nextInt();int sum 0;for(i…

Linux系统的进程,看完它,相信你想kill“it“就kill“it“o((>ω< ))o

Linux系统的进程 众所周知&#xff0c;不管在什么系统中&#xff0c;进程和线程都是操作系统中高并发处理中产生了重要的作用。Linux作为一个操作系统&#xff0c;也必须要拥有这两者才可以进行高并发。 进程是资源分配的最小单位。每一个进程都是相互独立的&#xff0c;不管是…

WordPress供求插件API文档:用户登录

该文档为WordPress供求插件文档&#xff0c;详情请查看 WordPress供求插件&#xff1a;一款专注于同城生活信息发布的插件-CSDN博客文章浏览阅读67次。WordPress供求插件&#xff1a;sliver-urban-life 是一款专注于提供同城生活信息发布与查看的插件&#xff0c;该插件可以实…

TCP收发——计算机网络——day02

今天主要讲了TCP的收发 TCP发端步骤 ①socket ②connect ③send ④closeTCP收端步骤 ①socket ②bind ③listen ④accept ⑤recv ⑥clise其函数主要有 connect int connect(int sockfd, const struct sockaddr *addr,socklen_t addrlen);功能:发送链接请求参数:sockfd:套接…

【设计数据密集型应用】复制

&#x1f44f;作者简介&#xff1a;大家好&#xff0c;我是爱敲代码的小黄&#xff0c;阿里淘天Java开发工程师&#xff0c;CSDN博客专家&#x1f4d5;系列专栏&#xff1a;Spring源码、Netty源码、Kafka源码、JUC源码、dubbo源码系列&#x1f525;如果感觉博主的文章还不错的话…

nginx 使用记录

使用&#xff1a; 去官网下载指定版本1.24.0&#xff0c;直接将压缩包解压到指定目录&#xff0c;打开cmd即可启动 启动服务器&#xff1a; 方法一&#xff1a;打开cmd命令窗口&#xff0c;切换到nginx解压目录下&#xff0c;输入命令start nginx &#xff0c;回车即可&…

数据结构:AVL树

目录 1、AVL树的概念 2、二叉搜索树的功能与实现 1、AVL树节点定义 2、AVL树的插入 3、AVL树的旋转操作 1、左旋 2、右旋 3、左右旋 4、右左旋 3、AVL树完整代码实现 1、AVL树的概念 在前面的文章中&#xff0c;我们学过了二叉搜索树&#xff0c;二叉搜索树虽可以缩短查…

智能控制:物联网智能插座对接文档

介绍 一开始买的某米的插座&#xff0c;但是好像接口不开放&#xff0c;所以找到了这个插座&#xff0c;然后自己开发了下&#xff0c;用接口控制插座开关。wifi的连接方式&#xff0c;通电后一般几秒后就会连接上wifi&#xff0c;这个时候通过接口发送命令给他。 产品图片 通…

cesium-天际线

主要是两个着色器 let postProccessStage new Cesium.PostProcessStage({//unform着色器对象 textureScalefragmentShader:// 声明一个纹理采样器 colorTexture 用于读取纹理颜色uniform sampler2D colorTexture; // 声明一个纹理采样器 depthTexture 用于读取深度纹理unifor…

windows安装程序无法将windows配置为此计算机

目录 问题描述 问题原因 解决办法 方法一 方法二 方法三&#xff1a; 问题描述 重装系统时显示windows安装程序无法将windows配置在此计算机硬件上. 问题原因 安装介质已损坏 如果可引导的安装介质&#xff08;如DVD或USB驱动器&#xff09;损坏或损坏&#xff0c;安装过…

XXE-XML实体注入漏洞

目录 1.xml基础 1.1什么是xml 1.2xml文档结构 1.3 什么是DTD 1.4 什么是实体 1.5 什么是外部实体 2.xxe漏洞 2.1xxe漏洞基本介绍 2.2xxe漏洞的危害 经典漏洞案例分析 3.xxe漏洞挖掘和利用 3.1. 识别潜在的XML入口 3.2. 检查XML处理逻辑 3.3. 构造试探Payload 常…

CVE-2024-25600 WordPress Bricks Builder RCE-漏洞分析研究

本次代码审计项目为PHP语言&#xff0c;我将继续以漏洞挖掘者的视角来分析漏洞的产生&#xff0c;调用与利用..... 前方高能&#xff0c;小伙伴们要真正仔细看咯..... 漏洞简介 CVE-2024-25600 是一个严重的&#xff08;CVSS 评分 9.8&#xff09;远程代码执行 (RCE) 漏洞&am…

软件设计师13--进程调度

软件设计师13--进程调度 考点1&#xff1a;PV操作的概念进程的同步与互斥PV操作例题&#xff1a; 考点2&#xff1a;信号量与PV操作进程管理 - PV操作与互斥模型进程管理 - PV操作与同步模型进程管理 - 互斥与同步模型结合例题&#xff1a; 考点3&#xff1a;前趋图与PV操作进程…