关于字符串统计的算法?

superfrank0711 2011-03-31 04:32:31
通过C#编程:有字符串cccbeebbb,通过编码生成:c3b1e2b3,请写一个方法实现其编码?求教各位大侠!
...全文
181 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
davidcoffee 2011-04-01
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestCount
{
class Program
{
static void Main(string[] args)
{
string targetString = "cccbeebbb";
StringBuilder sb = new StringBuilder();
List<string> resultList = new List<string>();
for (int i = 0; i < targetString.Length; i++)
{
var result = from target in targetString
where target == targetString[i]
select target;
if (result.Count() > 1)
{
if (!resultList.Contains(targetString[i].ToString()))
{
resultList.Add(targetString[i].ToString());
resultList.Add(result.Count().ToString());
}
}
}
foreach (string item in resultList)
{
sb.Append(item);
}
Console.WriteLine(sb.ToString());
Console.ReadLine();
}
}
}
哈哈用LINQ的方法不过貌似不能达到你相同字母的计数还分开来的效果,明天在想下回复你哈哈~
cfvgodot 2011-04-01
  • 打赏
  • 举报
回复
恩 其实你不用新建实例,只要改变前一实例的属性,然后ADD就可以了,相同的话就覆盖LIST里边的T就行了!
cfvgodot 2011-04-01
  • 打赏
  • 举报
回复
建个CLASS,里边两个属性,CHAR NAME,INT COUNT。。。LIST<CLASS>

遍历字符串,SUBSTRING,取1位长,如果一致在上一个实例里边的COUNT+1如果不一致新建实例然后ADD到LIST里边

最后输出字符串遍历LIST里边T的属性就可以了!

比楼上的兄台少不了几行代码,你这问题得去算法版找牛人去!不然你转成ASCII捅咕捅咕?
huwei001982 2011-04-01
  • 打赏
  • 举报
回复
都搞这个复杂?

string result = Regex.Replace("cccbdddeee", @"(\w)\1*", delegate(Match m)
{
return m.Groups[1].Value + m.Groups[0].Length
});
superfrank0711 2011-04-01
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 coregao0220 的回复:]
C# code
static void Main(string[] args)
{
string str = "cccbeebbb";

char[] charStr = str.ToCharArray();

string afterStr = "";

int co……
[/Quote]

思路比较明确不错 !除了这种方式有没有别的简单的方法啊?
newxdlysk 2011-04-01
  • 打赏
  • 举报
回复
(?is)([a-z])\1*匹配一下就可以了,长度就是每个匹配结果字符串的长度
cfvgodot 2011-04-01
  • 打赏
  • 举报
回复
正则强人来了!!!楼上V5
coregao0220 2011-03-31
  • 打赏
  • 举报
回复
static void Main(string[] args)
{
string str = "cccbeebbb";

char[] charStr = str.ToCharArray();

string afterStr = "";

int count = 1;

for (int i = 0; i < charStr.Length; i++)
{
if (i == charStr.Length - 1)
{
afterStr += charStr[i] + count.ToString();
}
else
{
if (charStr[i] != charStr[i + 1])
{
afterStr += charStr[i] + count.ToString();
count = 1;
}
else
{
count++;
}
}
}

Console.WriteLine(afterStr);
Console.ReadKey();
}
xleibb 2011-03-31
  • 打赏
  • 举报
回复
金 以以以以以
superfrank0711 2011-03-31
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 winner2050 的回复:]
太容易了。
[/Quote]

求赐教?谢谢!想听听你的思路!
winner2050 2011-03-31
  • 打赏
  • 举报
回复
太容易了。
superfrank0711 2011-03-31
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ningweidong 的回复:]
只是简单的字符串理
[/Quote]

欢迎赐教 谢谢!
superfrank0711 2011-03-31
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 jayinit 的回复:]
C# code
class Program
{
static void Main(string[] args)
{
string test = "aaa,中国,人民,bbb,中国,服务,中国,aaa,ccc";
foreach (StringTimes st in GetStringAndTim……
[/Quote]
这位大侠 谢谢你贴这么多代码,你这个部分代码可以借鉴学习,但是你这个是统计出现的次数,我那个是统计连续在一起的次数,不是在整个字符串中出现的次数,谢谢!
superfrank0711 2011-03-31
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 fage_1982 的回复:]
怒了,坚决不回答。
都懒成这样了。。。。

LS保持队形,连思路都不想写了。
[/Quote]

这位大侠,话不能这样说,我之所以发帖发出来,你觉得我是没有思考多吗?
这个字符串处理,不只是统计出现的次数,必须是连续的,我的思路是用嵌套循环,是可以做出来但是感觉太冗余代码复杂,我想请教各位大侠有没有简单的方法?谢谢 我新手、菜鸟不要笑话!谢谢
  • 打赏
  • 举报
回复
  class Program
{
static void Main(string[] args)
{
string test = "aaa,中国,人民,bbb,中国,服务,中国,aaa,ccc";
foreach (StringTimes st in GetStringAndTimes(test))
{
Console.WriteLine(st.timers + "," + st.value);
}

//foreach (StringTimes st in OrderByTimers(GetStringAndTimes(test)))
//{
// Console.WriteLine(st.value + "-出现次数" + st.timers.ToString() + ";");
//}

Console.WriteLine("根据出现次数排序后:");
foreach (StringTimes st in OrderByTimers(GetStringAndTimes(test)))
{
Console.Write(st.value + " ");
}
Console.ReadKey();
}

//对StringTimes进行排序
static List<StringTimes> OrderByTimers(List<StringTimes> ST)
{
List<StringTimes> st = ST;
for (int i = 0; i < st.Count; i++)
{
int currentTimes = st[i].timers;
for (int j = i; j < st.Count; j++)
{
if (st[j].timers > currentTimes)
{
StringTimes tempInt = st[i];
st[i] = st[j];
st[j] = tempInt;
currentTimes = st[j].timers;
}
else
{
continue;
}
}
}

return st;
}

static List<StringTimes> GetStringAndTimes(string Str)
{
List<StringTimes> timesAndString = new List<StringTimes>();

string[] strArray = Str.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

foreach (string str in strArray)
{
bool hasShow = false;
foreach (StringTimes st in timesAndString)
{
if (str == st.value)
{
st.timers++;
hasShow = true;
break;
}
else
{
continue;
}
}
if (!hasShow)
timesAndString.Add(new StringTimes(1, str));
}

return timesAndString;
}

class StringTimes
{
public int timers;
public string value;

public StringTimes(int Timers, string Value)
{
this.timers = Timers;
this.value = Value;
}
}

}
huwei001982 2011-03-31
  • 打赏
  • 举报
回复
想做压缩程序吗, 呵呵
cy19851024 2011-03-31
  • 打赏
  • 举报
回复
就是啊,这种东西挺好玩的啊,自己想想去实现很有意思啊。
不过肯定有高人,用简单的办法吧。期待。
小猪飞飞 2011-03-31
  • 打赏
  • 举报
回复
怒了,坚决不回答。
都懒成这样了。。。。

LS保持队形,连思路都不想写了。
ningweidong 2011-03-31
  • 打赏
  • 举报
回复
只是简单的字符串理

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧