C#怎么求一个txt文件中的最长子串 在一个txt文件中查找字母顺序连续长度最长的字串并输出。 字母顺序连续长度最长的字串:指的是bcdefg等这种按字母顺序连续的 也可以是abcde或者hijklmnopq 总之是顺序最长的 求代码 c# 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 面试题?循环遍历一下就okPS:规则不是很明确,aaaa连续不?dcba算不?xyzabc是连续的不?大小写区分不? 曾经的面试题 没有答上来 aaaa不算连续 abcde hijklmnopq bcdefg这种才是连续的 转成ascii码1 判断是否依次递增2 判断是否在a-z之间符合上面两个条件就是连续字符串遍历一次就出来了 要在SQL的话就好啦 数据定库义A-Z字符窜,遍历TXT文件中的字符窜,查询条件like%字符% 符合的返回。。 然后获取他们的长度添加在一个INT集合中获取最大值呵呵、、 using System;using System.Collections.Generic;using System.Data;using System.IO;using System.Text;using System.Windows.Forms;namespace TstConsole{ public partial class Form1 : Form { // 组织映射关系到数据词典 private Dictionary<char, char> CharRelationMap = new Dictionary<char, char>(); public Form1() { //初始化关系词典 CharRelationMap.Add('a', 'b'); CharRelationMap.Add('b', 'c'); CharRelationMap.Add('c', 'd'); CharRelationMap.Add('d', 'e'); CharRelationMap.Add('e', 'f'); CharRelationMap.Add('f', 'g'); CharRelationMap.Add('g', 'h'); CharRelationMap.Add('h', 'i'); CharRelationMap.Add('i', 'j'); CharRelationMap.Add('j', 'k'); CharRelationMap.Add('k', 'l'); CharRelationMap.Add('l', 'm'); CharRelationMap.Add('m', 'n'); CharRelationMap.Add('n', 'o'); CharRelationMap.Add('o', 'p'); CharRelationMap.Add('p', 'q'); CharRelationMap.Add('q', 'r'); CharRelationMap.Add('r', 's'); CharRelationMap.Add('s', 't'); CharRelationMap.Add('t', 'u'); CharRelationMap.Add('u', 'v'); CharRelationMap.Add('v', 'w'); CharRelationMap.Add('w', 'x'); CharRelationMap.Add('x', 'y'); CharRelationMap.Add('y', 'z'); InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // 组织创建文件 string path = @"c:\temp\MyTest.txt"; // Delete the file if it exists. if (File.Exists(path)) { File.Delete(path); } //Create the file. using (FileStream fs = File.Create(path)) { AddText(fs, "This is some text"); AddText(fs, "This is some more text,"); AddText(fs, "\r\nand this is on a new line"); AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n"); for (int i = 1; i < 120; i++) { AddText(fs, Convert.ToChar(i).ToString()); } } //Open the stream and read it back. long posBack; int ch_start; long currln, maxLn = -1, maxPos = -1; using (FileStream fs = File.OpenRead(path)) { fs.Seek(0, SeekOrigin.Begin); posBack = fs.Position; while ((ch_start = fs.ReadByte()) > 0) { currln = 0; ChkChar((char)ch_start, fs, ref currln); if (currln > maxLn) { maxPos = posBack; posBack += currln; maxLn = currln+1; } if ((posBack + currln) > fs.Length) break; fs.Seek(posBack + currln+1, SeekOrigin.Begin); posBack = fs.Position; //Console.Write((char)ch_start); } byte[] dataArray = new byte[100000]; fs.Seek(maxPos, SeekOrigin.Begin); for (int i = 0; i < maxLn; i++) { dataArray[i] = (byte)fs.ReadByte(); } ASCIIEncoding encoding = new ASCIIEncoding(); string constructedString = encoding.GetString(dataArray); } } private bool ChkChar(char ch_start, FileStream fs, ref long currln) { int ch_tmp = fs.ReadByte(); if (ch_tmp > 0) { if (CharRelationMap.ContainsKey(ch_start)) { if (ch_tmp == CharRelationMap[ch_start]) { currln++; ChkChar((char)ch_tmp, fs, ref currln); } } } else { } return true; } private void AddText(FileStream fs, string value) { byte[] info = new UTF8Encoding(true).GetBytes(value); fs.Write(info, 0, info.Length); } }}测试过了.. static void Main(string[] args) { string str = "avvvabccdbabcdefghaa"; char[] c = str.ToCharArray(); int max = 0; int l = 0; int temp=-1; foreach(char c1 in c) { int intAsciiCode = Convert.ToInt32(c1); if (temp != -1) { if (intAsciiCode - temp != 1) { if (max < l) { max = l; } l = 1; } else { l++; } } else { l = 1; } temp = intAsciiCode; } Console.WriteLine("最长的连续长度:" + max); Console.ReadKey(); }根据ASCII码来做比较 完整代码 /// <summary> /// 从小往大找(高效率) /// </summary> /// <param name="s1"></param> /// <param name="s2"></param> /// <returns></returns> static string GetMaxSubStr2(string s1, string s2) { // string s1 = "likeyouknn"; // string s2 = "lookyoummm"; string matchStr = "";//用来临时储存将要匹配的字符串 string result = ""; int maxCount = 0; for (int j = 1; j <= s1.Length+1; j++) //取长度 { //取开始位置索引 for (int i = 0; i <= s1.Length - j; i++) { globe++; //if (j < maxCount) break; matchStr = s1.Substring(i, j); if (s2.Contains(matchStr)) { if (maxCount < matchStr.Length) { maxCount = matchStr.Length; result = matchStr; } else { break; } } } } return result; }试这个吧,以前面试题碰到过,研究了大半天才找到一个效率比较高的 string str = "avvvabccdbuvwxyzabcdefgaa"; string maxstr = ""; int j = 0; for (int i = 1; i < str.Length; i++) { if ((int)str[i] == 97 + ((int)str[i - 1] + 1 - 97) % 26) continue; else { if (maxstr.Length < i - j) maxstr = str.Substring(j, i - j); j = i; } } build events 如何拷贝一个目录到bin\debug\ 如何让DataTable_ColumnChanged只触发一次 有没有可能用ADO.NET把SQLServer数据库中的Stored Procedure 名称、参数 全部列出来? 如何读取iframe页面中的变量值 关于在DataTable中查询时间的问题 如何判断一个type是list呢? datagridview没显示值的问题 关于dll的调用。 DataGrid翻页问题!高手帮帮忙啊! 关于int型数的操作问题 c# 将dataset导出到excel 同列数据相同 则合并单元格 如何使用C#开发“类ActiveX组件”
PS:规则不是很明确,aaaa连续不?dcba算不?xyzabc是连续的不?大小写区分不?
1 判断是否依次递增
2 判断是否在a-z之间
符合上面两个条件就是连续字符串遍历一次就出来了
数据定库义A-Z字符窜,遍历TXT文件中的字符窜,查询条件like%字符% 符合的返回。。
然后获取他们的长度添加在一个INT集合中获取最大值呵呵、、
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Windows.Forms;namespace TstConsole
{
public partial class Form1 : Form
{
// 组织映射关系到数据词典
private Dictionary<char, char> CharRelationMap = new Dictionary<char, char>(); public Form1()
{
//初始化关系词典
CharRelationMap.Add('a', 'b');
CharRelationMap.Add('b', 'c');
CharRelationMap.Add('c', 'd');
CharRelationMap.Add('d', 'e');
CharRelationMap.Add('e', 'f');
CharRelationMap.Add('f', 'g');
CharRelationMap.Add('g', 'h');
CharRelationMap.Add('h', 'i');
CharRelationMap.Add('i', 'j');
CharRelationMap.Add('j', 'k');
CharRelationMap.Add('k', 'l');
CharRelationMap.Add('l', 'm');
CharRelationMap.Add('m', 'n');
CharRelationMap.Add('n', 'o');
CharRelationMap.Add('o', 'p');
CharRelationMap.Add('p', 'q');
CharRelationMap.Add('q', 'r');
CharRelationMap.Add('r', 's');
CharRelationMap.Add('s', 't');
CharRelationMap.Add('t', 'u');
CharRelationMap.Add('u', 'v');
CharRelationMap.Add('v', 'w');
CharRelationMap.Add('w', 'x');
CharRelationMap.Add('x', 'y');
CharRelationMap.Add('y', 'z'); InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
// 组织创建文件
string path = @"c:\temp\MyTest.txt"; // Delete the file if it exists.
if (File.Exists(path))
{
File.Delete(path);
} //Create the file.
using (FileStream fs = File.Create(path))
{
AddText(fs, "This is some text");
AddText(fs, "This is some more text,");
AddText(fs, "\r\nand this is on a new line");
AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n"); for (int i = 1; i < 120; i++)
{
AddText(fs, Convert.ToChar(i).ToString());
}
} //Open the stream and read it back.
long posBack;
int ch_start;
long currln, maxLn = -1, maxPos = -1; using (FileStream fs = File.OpenRead(path))
{
fs.Seek(0, SeekOrigin.Begin);
posBack = fs.Position;
while ((ch_start = fs.ReadByte()) > 0)
{
currln = 0;
ChkChar((char)ch_start, fs, ref currln);
if (currln > maxLn)
{
maxPos = posBack;
posBack += currln;
maxLn = currln+1;
}
if ((posBack + currln) > fs.Length) break; fs.Seek(posBack + currln+1, SeekOrigin.Begin);
posBack = fs.Position;
//Console.Write((char)ch_start);
} byte[] dataArray = new byte[100000];
fs.Seek(maxPos, SeekOrigin.Begin);
for (int i = 0; i < maxLn; i++)
{
dataArray[i] = (byte)fs.ReadByte();
}
ASCIIEncoding encoding = new ASCIIEncoding(); string constructedString = encoding.GetString(dataArray);
}
} private bool ChkChar(char ch_start, FileStream fs, ref long currln)
{
int ch_tmp = fs.ReadByte();
if (ch_tmp > 0)
{
if (CharRelationMap.ContainsKey(ch_start))
{
if (ch_tmp == CharRelationMap[ch_start])
{
currln++;
ChkChar((char)ch_tmp, fs, ref currln);
}
}
}
else
{
} return true;
} private void AddText(FileStream fs, string value)
{
byte[] info = new UTF8Encoding(true).GetBytes(value);
fs.Write(info, 0, info.Length);
} }
}测试过了..
{
string str = "avvvabccdbabcdefghaa";
char[] c = str.ToCharArray(); int max = 0;
int l = 0;
int temp=-1;
foreach(char c1 in c)
{
int intAsciiCode = Convert.ToInt32(c1);
if (temp != -1)
{
if (intAsciiCode - temp != 1)
{
if (max < l)
{
max = l;
}
l = 1;
}
else
{
l++;
}
}
else
{
l = 1;
}
temp = intAsciiCode;
} Console.WriteLine("最长的连续长度:" + max);
Console.ReadKey();
}
根据ASCII码来做比较 完整代码
/// <summary>
/// 从小往大找(高效率)
/// </summary>
/// <param name="s1"></param>
/// <param name="s2"></param>
/// <returns></returns>
static string GetMaxSubStr2(string s1, string s2)
{
// string s1 = "likeyouknn";
// string s2 = "lookyoummm";
string matchStr = "";//用来临时储存将要匹配的字符串
string result = "";
int maxCount = 0;
for (int j = 1; j <= s1.Length+1; j++) //取长度
{
//取开始位置索引
for (int i = 0; i <= s1.Length - j; i++)
{
globe++;
//if (j < maxCount) break;
matchStr = s1.Substring(i, j);
if (s2.Contains(matchStr))
{
if (maxCount < matchStr.Length)
{
maxCount = matchStr.Length;
result = matchStr;
}
else
{
break;
} }
}
}
return result;
}
试这个吧,以前面试题碰到过,研究了大半天才找到一个效率比较高的
string maxstr = "";
int j = 0;
for (int i = 1; i < str.Length; i++)
{
if ((int)str[i] == 97 + ((int)str[i - 1] + 1 - 97) % 26)
continue;
else
{
if (maxstr.Length < i - j)
maxstr = str.Substring(j, i - j);
j = i;
}
}