C# 查找字符串行中出现的第一个最长字符 int lines=File.ReadAllLines(Path).Length;//计数行索引 公共字符串输入=”; public string mtd()//此方法应返回字符的第一个长出现索引(ry)和最长出现索引(rx) { Dictionary dict=新字典(); int max=0; foreach(输入中的字符c) { int i; dict.TryGetValue(c,out i); i++; 如果(i>max) { max=i; } dict[c]=i; } 字符串rx=“”; 字符串ry=“”; foreach(dict中的KeyValuePair字符) { 字符串x=chars.Key.ToString(); 字符串y=chars.Value.ToString(); 如果(字符值==最大值) { rx=x; y=y; } } 返回接收; }
我的目标是: 使用OpenFileDialog通过按钮获取一个file.txt(其中有更多的文本行) Usa另一个按钮,用于在richTextBox中显示每行的第一个最长子字符串(字符出现)和第一个最长字符出现的索引(从零开始) 这就是我的意思的一个例子 在文本文件中,我有三行: 阿贝布 CCDDD efffgggg 结果应为: aaa,0 DDD,1 ggggg,4C# 查找字符串行中出现的第一个最长字符 int lines=File.ReadAllLines(Path).Length;//计数行索引 公共字符串输入=”; public string mtd()//此方法应返回字符的第一个长出现索引(ry)和最长出现索引(rx) { Dictionary dict=新字典(); int max=0; foreach(输入中的字符c) { int i; dict.TryGetValue(c,out i); i++; 如果(i>max) { max=i; } dict[c]=i; } 字符串rx=“”; 字符串ry=“”; foreach(dict中的KeyValuePair字符) { 字符串x=chars.Key.ToString(); 字符串y=chars.Value.ToString(); 如果(字符值==最大值) { rx=x; y=y; } } 返回接收; },c#,.net,winforms,C#,.net,Winforms,我的目标是: 使用OpenFileDialog通过按钮获取一个file.txt(其中有更多的文本行) Usa另一个按钮,用于在richTextBox中显示每行的第一个最长子字符串(字符出现)和第一个最长字符出现的索引(从零开始) 这就是我的意思的一个例子 在文本文件中,我有三行: 阿贝布 CCDDD efffgggg 结果应为: aaa,0 DDD,1 ggggg,4 我见过关于这个问题的其他问题,但我没有找到解决办法。有什么想法吗?下面是一个函数,可以为给定的行执行您想要的操作: in
我见过关于这个问题的其他问题,但我没有找到解决办法。有什么想法吗?下面是一个函数,可以为给定的行执行您想要的操作:
int lines = File.ReadAllLines(Path).Length; // count lines index
public string input = "";
public string mtd() // This method should return the first long occurrence index (ry) and the longest occurrence of the char (rx)
{
Dictionary<char, int> dict = new Dictionary<char, int>();
int max = 0;
foreach (char c in input)
{
int i;
dict.TryGetValue(c, out i);
i++;
if (i > max)
{
max = i;
}
dict[c] = i;
}
string rx = "";
string ry = "";
foreach (KeyValuePair<char, int> chars in dict)
{
string x = chars.Key.ToString();
string y = chars.Value.ToString();
if (chars.Value == max)
{
rx = x;
ry = y;
}
}
return rx;
}
用途如下:
public static string GetCharacterRepetitionAndPosition(string s)
{
if (string.IsNullOrWhiteSpace(s))
return s;
var result = (from ch in s
group ch by ch into g
select new { Cnt = g.Count(), Ch = g.Key });
var maxCnt = -1;
char theMaxChar =char.MinValue;
int howManyCharacters;
foreach (var item in result)
{
if (item.Cnt > maxCnt)
{
maxCnt = item.Cnt;
theMaxChar = item.Ch;
howManyCharacters = item.Cnt;
}
}
var idx = s.IndexOf(theMaxChar);
return new string(theMaxChar,maxCnt) + "," + idx;
}
算法复杂性分析:
1) 通过groupby选择的值为O(N)
2) 结果上的foreach也是O(N)
3) IndexOf()调用是O(N)
所以总的复杂性(大O)是O(N)
这里发生了什么:
1) 首先,我们将所有角色按其幻影进行分组,并计算其中有多少人在一个组中
2) 我们迭代这个结果,记住一个字符的最大隔离数,以及该字符是什么
3) 我们返回一个字符串(具有最高幻影的字符)下面是一个函数,它为给定的行执行所需的操作:
int lines = File.ReadAllLines(Path).Length; // count lines index
public string input = "";
public string mtd() // This method should return the first long occurrence index (ry) and the longest occurrence of the char (rx)
{
Dictionary<char, int> dict = new Dictionary<char, int>();
int max = 0;
foreach (char c in input)
{
int i;
dict.TryGetValue(c, out i);
i++;
if (i > max)
{
max = i;
}
dict[c] = i;
}
string rx = "";
string ry = "";
foreach (KeyValuePair<char, int> chars in dict)
{
string x = chars.Key.ToString();
string y = chars.Value.ToString();
if (chars.Value == max)
{
rx = x;
ry = y;
}
}
return rx;
}
用途如下:
public static string GetCharacterRepetitionAndPosition(string s)
{
if (string.IsNullOrWhiteSpace(s))
return s;
var result = (from ch in s
group ch by ch into g
select new { Cnt = g.Count(), Ch = g.Key });
var maxCnt = -1;
char theMaxChar =char.MinValue;
int howManyCharacters;
foreach (var item in result)
{
if (item.Cnt > maxCnt)
{
maxCnt = item.Cnt;
theMaxChar = item.Ch;
howManyCharacters = item.Cnt;
}
}
var idx = s.IndexOf(theMaxChar);
return new string(theMaxChar,maxCnt) + "," + idx;
}
算法复杂性分析:
1) 通过groupby选择的值为O(N)
2) 结果上的foreach也是O(N)
3) IndexOf()调用是O(N)
所以总的复杂性(大O)是O(N)
这里发生了什么:
1) 首先,我们将所有角色按其幻影进行分组,并计算其中有多少人在一个组中
2) 我们迭代这个结果,记住一个字符的最大隔离数,以及该字符是什么
3) 我们返回一个字符串(具有最高幻影的字符)以下内容将给出您要求的结果,并在O(n)时间内运行
var行=新列表{“aaabb”、“ccddddd”、“efffgggg”};
foreach(行中的var行)
{
if(string.IsNullOrEmpty(line))//如果该行为null或空,则跳过它。
{
WriteLine(“空或空字符串”);
继续;
}
char prev=行[0];//看到的前一个字符以第一个字符开头
int maxSeen=0;//看到的最大连续字符数
int maxSeenIndex=-1;//最大可见字符的索引。
int currentSeen=1;//当前看到的连续字符数。
int currentSeenIndex=0;//当前字符的索引。
for(int i=1;imaxSeen)//检查所看到的电流是否大于最大值
{
maxSeen=currentSeen;
maxSeenIndex=当前SeenIndex;
}
currentSeen=1;//将当前seen重置为1
currentSeenIndex=i;//将当前seen索引设置为当前索引
}
prev=行[i];//将当前字符设置为上一个字符
}
如果(currentSeen>maxSeen)//必须再次执行此检查
{
maxSeen=currentSeen;
maxSeenIndex=当前SeenIndex;
}
Console.WriteLine(line.Substring(maxSeenIndex,maxSeen)+“,”+maxSeenIndex);
}
以下内容将给出您要求的结果,并在O(n)时间内运行
var行=新列表{“aaabb”、“ccddddd”、“efffgggg”};
foreach(行中的var行)
{
if(string.IsNullOrEmpty(line))//如果该行为null或空,则跳过它。
{
WriteLine(“空或空字符串”);
继续;
}
char prev=行[0];//看到的前一个字符以第一个字符开头
int maxSeen=0;//看到的最大连续字符数
int maxSeenIndex=-1;//最大可见字符的索引。
int currentSeen=1;//当前看到的连续字符数。
int currentSeenIndex=0;//当前字符的索引。
for(int i=1;imaxSeen)//检查所看到的电流是否大于最大值
{
maxSeen=currentSeen;
maxSeenIndex=当前SeenIndex;
}
currentSeen=1;//将当前seen重置为1
currentSeenIndex=i;//将当前seen索引设置为cu
(.)(\1+)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
public class Program
{
public static void Main()
{
//var lines = File.ReadAllLines("C:\\text.txt");
var lines = new List<string> { "aaabb", "ccddddd", "efffggggg" };
var result = (
from line in lines
let matches = Regex.Matches(line, "(.)\\1+").Cast<Match>()
let maxLen = matches.Max(match => match.Length)
let maxMatch = matches.First(match => match.Length == maxLen)
let index = line.IndexOf(maxMatch.Value)
select string.Format("{0},{1}", maxMatch.Value, index)
).ToList();
result.ForEach(Console.WriteLine);
}
}
}