C# 计算字符串/文件的哈希值(MD5、SHA)

时间:2022-09-07 17:06:35

原文 C# 计算字符串的哈希值(MD5、SHA)

已做修改

一、关于本文

本文中是一个类库,包括下面几个函数:

/// 1)计算32位MD5码(大小写):Hash_MD5_32
/// 2)计算16位MD5码(大小写):Hash_MD5_16
/// 3)计算32位2重MD5码(大小写):Hash_2_MD5_32
/// 4)计算16位2重MD5码(大小写):Hash_2_MD5_16
/// 5)计算SHA-1码(大小写):Hash_SHA_1
/// 6)计算SHA-256码(大小写):Hash_SHA_256
/// 7)计算SHA-384码(大小写):Hash_SHA_384
/// 8)计算SHA-512码(大小写):Hash_SHA_512
/// 9)计算文件的 SHA256 值:SHA256File
/// 10)计算文件的 MD5 值:MD5File
/// 11)计算文件的 SHA1 值:SHA1File

编译后被打包成文件HashTools.dll,其他程序可以在添加引用后对这些函数进行调用

二、类库中各函数代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace HashTool
{
/// <summary>
/// C# 计算字符串/文件哈希值(MD5、SHA)
/// 1)计算32位MD5码(大小写):Hash_MD5_32
/// 2)计算16位MD5码(大小写):Hash_MD5_16
/// 3)计算32位2重MD5码(大小写):Hash_2_MD5_32
/// 4)计算16位2重MD5码(大小写):Hash_2_MD5_16
/// 5)计算SHA-1码(大小写):Hash_SHA_1
/// 6)计算SHA-256码(大小写):Hash_SHA_256
/// 7)计算SHA-384码(大小写):Hash_SHA_384
/// 8)计算SHA-512码(大小写):Hash_SHA_512
/// 9)计算文件的 SHA256 值:SHA256File
/// 10)计算文件的 MD5 值:MD5File
/// 11)计算文件的 SHA1 值:SHA1File
/// </summary>
public class HashHelper
{ /// <summary>
/// 计算32位MD5码
/// </summary>
/// <param name="word">字符串</param>
/// <param name="toUpper">返回哈希值格式 true:英文大写,false:英文小写</param>
/// <returns></returns>
public static string Hash_MD5_32(string word, bool toUpper = true)
{
try
{
System.Security.Cryptography.MD5CryptoServiceProvider MD5CSP
= new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word);
byte[] bytHash = MD5CSP.ComputeHash(bytValue);
MD5CSP.Clear(); //根据计算得到的Hash码翻译为MD5码
string sHash = "", sTemp = "";
for (int counter = ; counter < bytHash.Count(); counter++)
{
long i = bytHash[counter] / ;
if (i > )
{
sTemp = ((char)(i - + 0x41)).ToString();
}
else
{
sTemp = ((char)(i + 0x30)).ToString();
}
i = bytHash[counter] % ;
if (i > )
{
sTemp += ((char)(i - + 0x41)).ToString();
}
else
{
sTemp += ((char)(i + 0x30)).ToString();
}
sHash += sTemp;
} //根据大小写规则决定返回的字符串
return toUpper ? sHash : sHash.ToLower();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// 计算16位MD5码
/// </summary>
/// <param name="word">字符串</param>
/// <param name="toUpper">返回哈希值格式 true:英文大写,false:英文小写</param>
/// <returns></returns>
public static string Hash_MD5_16(string word, bool toUpper = true)
{
try
{
string sHash = Hash_MD5_32(word).Substring(, );
return toUpper ? sHash : sHash.ToLower();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
} /// <summary>
/// 计算32位2重MD5码
/// </summary>
/// <param name="word">字符串</param>
/// <param name="toUpper">返回哈希值格式 true:英文大写,false:英文小写</param>
/// <returns></returns>
public static string Hash_2_MD5_32(string word, bool toUpper = true)
{
try
{
System.Security.Cryptography.MD5CryptoServiceProvider MD5CSP
= new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word);
byte[] bytHash = MD5CSP.ComputeHash(bytValue); //根据计算得到的Hash码翻译为MD5码
string sHash = "", sTemp = "";
for (int counter = ; counter < bytHash.Count(); counter++)
{
long i = bytHash[counter] / ;
if (i > )
{
sTemp = ((char)(i - + 0x41)).ToString();
}
else
{
sTemp = ((char)(i + 0x30)).ToString();
}
i = bytHash[counter] % ;
if (i > )
{
sTemp += ((char)(i - + 0x41)).ToString();
}
else
{
sTemp += ((char)(i + 0x30)).ToString();
}
sHash += sTemp;
} bytValue = System.Text.Encoding.UTF8.GetBytes(sHash);
bytHash = MD5CSP.ComputeHash(bytValue);
MD5CSP.Clear();
sHash = ""; //根据计算得到的Hash码翻译为MD5码
for (int counter = ; counter < bytHash.Count(); counter++)
{
long i = bytHash[counter] / ;
if (i > )
{
sTemp = ((char)(i - + 0x41)).ToString();
}
else
{
sTemp = ((char)(i + 0x30)).ToString();
}
i = bytHash[counter] % ;
if (i > )
{
sTemp += ((char)(i - + 0x41)).ToString();
}
else
{
sTemp += ((char)(i + 0x30)).ToString();
}
sHash += sTemp;
} //根据大小写规则决定返回的字符串
return toUpper ? sHash : sHash.ToLower();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
} /// <summary>
/// 计算16位2重MD5码
/// </summary>
/// <param name="word">字符串</param>
/// <param name="toUpper">返回哈希值格式 true:英文大写,false:英文小写</param>
/// <returns></returns>
public static string Hash_2_MD5_16(string word, bool toUpper = true)
{
try
{
System.Security.Cryptography.MD5CryptoServiceProvider MD5CSP
= new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word);
byte[] bytHash = MD5CSP.ComputeHash(bytValue); //根据计算得到的Hash码翻译为MD5码
string sHash = "", sTemp = "";
for (int counter = ; counter < bytHash.Count(); counter++)
{
long i = bytHash[counter] / ;
if (i > )
{
sTemp = ((char)(i - + 0x41)).ToString();
}
else
{
sTemp = ((char)(i + 0x30)).ToString();
}
i = bytHash[counter] % ;
if (i > )
{
sTemp += ((char)(i - + 0x41)).ToString();
}
else
{
sTemp += ((char)(i + 0x30)).ToString();
}
sHash += sTemp;
} sHash = sHash.Substring(, ); bytValue = System.Text.Encoding.UTF8.GetBytes(sHash);
bytHash = MD5CSP.ComputeHash(bytValue);
MD5CSP.Clear();
sHash = ""; //根据计算得到的Hash码翻译为MD5码
for (int counter = ; counter < bytHash.Count(); counter++)
{
long i = bytHash[counter] / ;
if (i > )
{
sTemp = ((char)(i - + 0x41)).ToString();
}
else
{
sTemp = ((char)(i + 0x30)).ToString();
}
i = bytHash[counter] % ;
if (i > )
{
sTemp += ((char)(i - + 0x41)).ToString();
}
else
{
sTemp += ((char)(i + 0x30)).ToString();
}
sHash += sTemp;
} sHash = sHash.Substring(, ); //根据大小写规则决定返回的字符串
return toUpper ? sHash : sHash.ToLower();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// 计算SHA-1码
/// </summary>
/// <param name="word">字符串</param>
/// <param name="toUpper">返回哈希值格式 true:英文大写,false:英文小写</param>
/// <returns></returns>
public static string Hash_SHA_1(string word, bool toUpper = true)
{
try
{
System.Security.Cryptography.SHA1CryptoServiceProvider SHA1CSP
= new System.Security.Cryptography.SHA1CryptoServiceProvider(); byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word);
byte[] bytHash = SHA1CSP.ComputeHash(bytValue);
SHA1CSP.Clear(); //根据计算得到的Hash码翻译为SHA-1码
string sHash = "", sTemp = "";
for (int counter = ; counter < bytHash.Count(); counter++)
{
long i = bytHash[counter] / ;
if (i > )
{
sTemp = ((char)(i - + 0x41)).ToString();
}
else
{
sTemp = ((char)(i + 0x30)).ToString();
}
i = bytHash[counter] % ;
if (i > )
{
sTemp += ((char)(i - + 0x41)).ToString();
}
else
{
sTemp += ((char)(i + 0x30)).ToString();
}
sHash += sTemp;
} //根据大小写规则决定返回的字符串
return toUpper ? sHash : sHash.ToLower();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// 计算SHA-256码
/// </summary>
/// <param name="word">字符串</param>
/// <param name="toUpper">返回哈希值格式 true:英文大写,false:英文小写</param>
/// <returns></returns>
public static string Hash_SHA_256(string word, bool toUpper = true)
{
try
{
System.Security.Cryptography.SHA256CryptoServiceProvider SHA256CSP
= new System.Security.Cryptography.SHA256CryptoServiceProvider(); byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word);
byte[] bytHash = SHA256CSP.ComputeHash(bytValue);
SHA256CSP.Clear(); //根据计算得到的Hash码翻译为SHA-1码
string sHash = "", sTemp = "";
for (int counter = ; counter < bytHash.Count(); counter++)
{
long i = bytHash[counter] / ;
if (i > )
{
sTemp = ((char)(i - + 0x41)).ToString();
}
else
{
sTemp = ((char)(i + 0x30)).ToString();
}
i = bytHash[counter] % ;
if (i > )
{
sTemp += ((char)(i - + 0x41)).ToString();
}
else
{
sTemp += ((char)(i + 0x30)).ToString();
}
sHash += sTemp;
} //根据大小写规则决定返回的字符串
return toUpper ? sHash : sHash.ToLower();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// 计算SHA-384码
/// </summary>
/// <param name="word">字符串</param>
/// <param name="toUpper">返回哈希值格式 true:英文大写,false:英文小写</param>
/// <returns></returns>
public static string Hash_SHA_384(string word, bool toUpper = true)
{
try
{
System.Security.Cryptography.SHA384CryptoServiceProvider SHA384CSP
= new System.Security.Cryptography.SHA384CryptoServiceProvider(); byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word);
byte[] bytHash = SHA384CSP.ComputeHash(bytValue);
SHA384CSP.Clear(); //根据计算得到的Hash码翻译为SHA-1码
string sHash = "", sTemp = "";
for (int counter = ; counter < bytHash.Count(); counter++)
{
long i = bytHash[counter] / ;
if (i > )
{
sTemp = ((char)(i - + 0x41)).ToString();
}
else
{
sTemp = ((char)(i + 0x30)).ToString();
}
i = bytHash[counter] % ;
if (i > )
{
sTemp += ((char)(i - + 0x41)).ToString();
}
else
{
sTemp += ((char)(i + 0x30)).ToString();
}
sHash += sTemp;
} //根据大小写规则决定返回的字符串
return toUpper ? sHash : sHash.ToLower();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// 计算SHA-512码
/// </summary>
/// <param name="word">字符串</param>
/// <param name="toUpper">返回哈希值格式 true:英文大写,false:英文小写</param>
/// <returns></returns>
public static string Hash_SHA_512(string word, bool toUpper = true)
{
try
{
System.Security.Cryptography.SHA512CryptoServiceProvider SHA512CSP
= new System.Security.Cryptography.SHA512CryptoServiceProvider(); byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(word);
byte[] bytHash = SHA512CSP.ComputeHash(bytValue);
SHA512CSP.Clear(); //根据计算得到的Hash码翻译为SHA-1码
string sHash = "", sTemp = "";
for (int counter = ; counter < bytHash.Count(); counter++)
{
long i = bytHash[counter] / ;
if (i > )
{
sTemp = ((char)(i - + 0x41)).ToString();
}
else
{
sTemp = ((char)(i + 0x30)).ToString();
}
i = bytHash[counter] % ;
if (i > )
{
sTemp += ((char)(i - + 0x41)).ToString();
}
else
{
sTemp += ((char)(i + 0x30)).ToString();
}
sHash += sTemp;
} //根据大小写规则决定返回的字符串
return toUpper ? sHash : sHash.ToLower();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
} /// <summary>
/// 计算文件的 SHA256 值
/// </summary>
/// <param name="fileStream">文件流</param>
/// <returns>System.String.</returns>
public static string SHA256File(System.IO.FileStream fileStream)
{
System.Security.Cryptography.SHA256 mySHA256 = System.Security.Cryptography.SHA256Managed.Create(); byte[] hashValue; // Create a fileStream for the file.
//FileStream fileStream = fInfo.Open(FileMode.Open);
// Be sure it's positioned to the beginning of the stream.
fileStream.Position = ;
// Compute the hash of the fileStream.
hashValue = mySHA256.ComputeHash(fileStream); // Close the file.
fileStream.Close();
// Write the hash value to the Console.
return PrintByteArray(hashValue); }
/// <summary>
/// 计算文件的 MD5 值
/// </summary>
/// <param name="fileName">要计算 MD5 值的文件名和路径</param>
/// <returns>MD5 值16进制字符串</returns>
public static string MD5File(string fileName)
{ return HashFile(fileName, "md5"); } /// <summary>
/// 计算文件的 sha1 值
/// </summary>
/// <param name="fileName">要计算 sha1 值的文件名和路径</param>
/// <returns>sha1 值16进制字符串</returns>
public static string SHA1File(string fileName)
{ return HashFile(fileName, "sha1"); } /// <summary>
/// 计算文件的哈希值
/// </summary>
/// <param name="fileName">要计算哈希值的文件名和路径</param>
/// <param name="algName">算法:sha1,md5</param>
/// <returns>哈希值16进制字符串</returns>
private static string HashFile(string fileName, string algName)
{ if (!System.IO.File.Exists(fileName)) return string.Empty; System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read); byte[] hashBytes = HashData(fs, algName); fs.Close(); return ByteArrayToHexString(hashBytes); } /// <summary>
/// 计算哈希值
/// </summary>
/// <param name="stream">要计算哈希值的 Stream</param>
/// <param name="algName">算法:sha1,md5</param>
/// <returns>哈希值字节数组</returns>
private static byte[] HashData(System.IO.Stream stream, string algName)
{ System.Security.Cryptography.HashAlgorithm algorithm; if (algName == null)
{ throw new ArgumentNullException("algName 不能为 null"); } if (string.Compare(algName, "sha1", true) == )
{ algorithm = System.Security.Cryptography.SHA1.Create(); } else
{ if (string.Compare(algName, "md5", true) != )
{ throw new Exception("algName 只能使用 sha1 或 md5"); } algorithm = System.Security.Cryptography.MD5.Create(); } return algorithm.ComputeHash(stream); } /// <summary>
/// 字节数组转换为16进制表示的字符串
/// </summary>
private static string ByteArrayToHexString(byte[] buf)
{ return BitConverter.ToString(buf).Replace("-", ""); } private static string PrintByteArray(byte[] array)
{
StringBuilder sb = new StringBuilder();
int i;
for (i = ; i < array.Length; i++)
{
sb.Append(String.Format("{0:X2}", array[i])); }
return sb.ToString();
} /// <summary>
/// 计算指定文件的CRC32值
/// </summary>
/// <param name="fileName">指定文件的完全限定名称</param>
/// <returns>返回值的字符串形式</returns>
public static String Crc32File(String fileName)
{
String hashCRC32 = String.Empty;
//检查文件是否存在,如果文件存在则进行计算,否则返回空值
if (System.IO.File.Exists(fileName))
{
using (System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
//计算文件的CSC32值
Crc32 calculator = new Crc32();
Byte[] buffer = calculator.ComputeHash(fs);
calculator.Clear();
//将字节数组转换成十六进制的字符串形式
StringBuilder stringBuilder = new StringBuilder();
for (int i = ; i < buffer.Length; i++)
{
stringBuilder.Append(buffer[i].ToString("X2"));
}
hashCRC32 = stringBuilder.ToString();
}//关闭文件流
}
return hashCRC32;
} #region Class CRC32 算法的实现
/// <summary>
/// 提供 CRC32 算法的实现
/// </summary>
private class Crc32 : System.Security.Cryptography.HashAlgorithm
{
public const UInt32 DefaultPolynomial = 0xedb88320;
public const UInt32 DefaultSeed = 0xffffffff;
private UInt32 hash;
private UInt32 seed;
private UInt32[] table;
private static UInt32[] defaultTable;
public Crc32()
{
table = InitializeTable(DefaultPolynomial);
seed = DefaultSeed;
Initialize();
}
public Crc32(UInt32 polynomial, UInt32 seed)
{
table = InitializeTable(polynomial);
this.seed = seed;
Initialize();
}
public override void Initialize()
{
hash = seed;
}
protected override void HashCore(byte[] buffer, int start, int length)
{
hash = CalculateHash(table, hash, buffer, start, length);
}
protected override byte[] HashFinal()
{
byte[] hashBuffer = UInt32ToBigEndianBytes(~hash);
this.HashValue = hashBuffer;
return hashBuffer;
}
public static UInt32 Compute(byte[] buffer)
{
return ~CalculateHash(InitializeTable(DefaultPolynomial), DefaultSeed, buffer, , buffer.Length);
}
public static UInt32 Compute(UInt32 seed, byte[] buffer)
{
return ~CalculateHash(InitializeTable(DefaultPolynomial), seed, buffer, , buffer.Length);
}
public static UInt32 Compute(UInt32 polynomial, UInt32 seed, byte[] buffer)
{
return ~CalculateHash(InitializeTable(polynomial), seed, buffer, , buffer.Length);
}
private static UInt32[] InitializeTable(UInt32 polynomial)
{
if (polynomial == DefaultPolynomial && defaultTable != null)
{
return defaultTable;
}
UInt32[] createTable = new UInt32[];
for (int i = ; i < ; i++)
{
UInt32 entry = (UInt32)i;
for (int j = ; j < ; j++)
{
if ((entry & ) == )
entry = (entry >> ) ^ polynomial;
else
entry = entry >> ;
}
createTable[i] = entry;
}
if (polynomial == DefaultPolynomial)
{
defaultTable = createTable;
}
return createTable;
}
private static UInt32 CalculateHash(UInt32[] table, UInt32 seed, byte[] buffer, int start, int size)
{
UInt32 crc = seed;
for (int i = start; i < size; i++)
{
unchecked
{
crc = (crc >> ) ^ table[buffer[i] ^ crc & 0xff];
}
}
return crc;
}
private byte[] UInt32ToBigEndianBytes(UInt32 x)
{
return new byte[] { (byte)((x >> ) & 0xff), (byte)((x >> ) & 0xff), (byte)((x >> ) & 0xff), (byte)(x & 0xff) };
}
} #endregion } }
 
三、函数调用
建立项目ComputeHash,添加对HashTools.dll库的引用。并添加代码:  
using HashTools;
然后在Main函数中添加下列代码:
static void Main(string[] args)
{
Console.WriteLine("MD5 of \"abc\""); Console.WriteLine("MD5_32(Upper): {0}",
HashHelper.Hash_MD5_32("abc"));
Console.WriteLine("MD5_32(Lower): {0}",
HashHelper.Hash_MD5_32("abc", false)); Console.WriteLine("MD5_16(Upper): {0}",
HashHelper.Hash_MD5_16("abc"));
Console.WriteLine("MD5_16(Lower): {0}",
HashHelper.Hash_MD5_16("abc", false)); Console.WriteLine("2_MD5_32(Upper): {0}",
HashHelper.Hash_2_MD5_32("abc"));
Console.WriteLine("2_MD5_32(Lower): {0}",
HashHelper.Hash_2_MD5_32("abc", false)); Console.WriteLine("2_MD5_32(Upper): {0}",
HashHelper.Hash_2_MD5_16("abc"));
Console.WriteLine("2_MD5_32(Lower): {0}",
HashHelper.Hash_2_MD5_16("abc", false)); Console.WriteLine("SHA of \"abc\""); Console.WriteLine("SHA-1(Upper): {0}",
HashHelper.Hash_SHA_1("abc"));
Console.WriteLine("SHA-1(Lower): {0}",
HashHelper.Hash_SHA_1("abc", false)); Console.WriteLine("SHA-256(Upper): {0}",
HashHelper.Hash_SHA_256("abc"));
Console.WriteLine("SHA-256(Lower): {0}",
HashHelper.Hash_SHA_256("abc", false)); Console.WriteLine("SHA-384(Upper): {0}",
HashHelper.Hash_SHA_384("abc"));
Console.WriteLine("SHA-384(Lower): {0}",
HashHelper.Hash_SHA_384("abc", false)); Console.WriteLine("SHA-512(Upper): {0}",
HashHelper.Hash_SHA_512("abc"));
Console.WriteLine("SHA-512(Lower): {0}",
HashHelper.Hash_SHA_512("abc", false)); Console.ReadLine();
}
运行结果如下:

C# 计算字符串/文件的哈希值(MD5、SHA) 
END

C# 计算字符串/文件的哈希值(MD5、SHA)的更多相关文章

  1. usb转串口驱动时会出现&OpenCurlyDoubleQuote;文件的哈希值不在指定的目录”这样的提示

    一般在安装一些usb转串口驱动时会出现“文件的哈希值不在指定的目录”这样的提示,那么怎么解决呢?知道的别喷我哦,我只是再普及一下,嘿嘿1.鼠标移到右下角,点击“设置”,再点击“更改电脑设置”2.点击最 ...

  2. WIN10下安装USB转串口驱动出现&OpenCurlyDoubleQuote;文件的哈希值不在指定的目录”的解决办法

    今天安装openJTAG驱动时出现“文件的哈希值不在指定的目录”,系统为WIN10专业版. 原因是驱动无数字签名,在WIN10中是不安全的驱动,所以显示哈希值不在范围内不能安装. 经查阅已经解决,发放 ...

  3. USB-Blaster CPLD FPGA Intel 驱动安装不上的问题,文件的哈希值不在指定的目录文件中,的解决办法,其实很简单

    intel的官网的驱动安装文档: https://www.intel.com/content/www/us/en/programmable/support/support-resources/down ...

  4. 文件的哈希值不在指定的目录文件中。此文件可能已损坏或被篡(Windows10 &sol;Windows8&period;1)

    ------------------------------------------Windows10------------------------------------------------ ...

  5. win8&period;1安装驱动出现&OpenCurlyDoubleQuote;文件的哈希值不在指定的目录”的解决办法&lbrack;zz&rsqb;

    1.鼠标移到右下角,点击“设置”,再点击“更改电脑设置”2.点击最后一个“更新和回复”,再点击“恢复”3.点击“恢复”之后,在右边点击高级启动下面的“重新启动”4.等一会会出现几个选项,点击“疑难解答 ...

  6. Java中String的哈希值计算

    下面都是从String类的源码中粘贴出来的 private int hash; // Default to 0 public int hashCode() { int h = hash; if (h ...

  7. USB-Blaster驱动安装失败——文件哈希值不在指定目录中

    右击此电脑,选择管理,选择设备管理器,更新USB-Blaster驱动出现问题 问题: 文件的哈希值不在指定的目录文件中,如图: 解决办法: Windows键+R→shutdown.exe /r /o ...

  8. 加解密(校验哈希值、MD5值)

    1.计算文本哈希值: public static string ComputeHash(string password) { byte[] buffer = System.Text.Encoding. ...

  9. poj1200 字符串hash 滚动哈希初探

    假如要判断字符串A“AABA”是否是字符串B“AABAACAADAABAABA”的子串 最朴素的算法是枚举B的所有长度为4的子串,然后逐个与A进行对比,这样的时间复杂度是O(mn),m为A的长度,n为 ...

随机推荐

  1. window&period;print&lpar;&rpar; 去掉页眉页脚及打印链接【转载】

    页面中添加样式: <style media="print"> @page { size: auto; /* auto is the initial value */ m ...

  2. 重新想象 Windows 8 Store Apps &lpar;64&rpar; - 后台任务&colon; 开发一个简单的后台任务

    [源码下载] 重新想象 Windows 8 Store Apps (64) - 后台任务: 开发一个简单的后台任务 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 后 ...

  3. 【现代程序设计】【homework-08】

    1. 理解C++变量的作用域和生命周期 #include<stdio.h> char * test() { ]="; return s; } main() { puts(test ...

  4. 【MS Office2013小技巧】Word中公式中的等号对齐

    步骤: 1. 先将所需要对齐的公式分不同行打出来: 2. 选中所有公式,右键点击并选择“对齐点(A) =”,如图 此时,如果能够正常对齐,则无需进行下面的步骤,但如果出现下图情况并未正常对齐的,再进行 ...

  5. js判断一个变量是否为数组的解决方案

    前端开发中,在做项目的时候,我们经常需要对一个变量进行数组类型的判断,当然即使你暂时没遇到,但是这个问题也是大家去面试时的高频问题,有必要拿出来说一说. 大家都知道js中可以使用typeof来判断变量 ...

  6. *的cocos2d-x学习笔记04-触摸点

    Touch position是屏幕坐标系中的点,OpenGL position是cocos2d-x用到的OpenGL坐标系上的点坐标.所以就需要将touch的坐标转换成OpenGL坐标系中的点坐标. ...

  7. 第七十九课 最短路径(Floyd)

    程序如下: #ifndef GRAPH_H #define GRAPH_H #include "Object.h" #include "SharedPointer.h&q ...

  8. LeetCode - 872&period; Leaf-Similar Trees

    Consider all the leaves of a binary tree.  From left to right order, the values of those leaves form ...

  9. 关于ASP&period;NET MVC4在IIS6中不认识安卓移动设备的解决办法

    在IIS7中发现安卓的手机浏览器是可以跳转滴. 项目中是采用***.mobile.cshtml来显示移动视图的. 部署到IIS6.0中发现并没有转到*.mobile.cshtml移动视图. 进过漫长的 ...

  10. python相关知识&sol;技巧文摘

    python文件和目录操作 python连接mysql数据库 Python字符编码详解 unicode相关介绍