题目:有字符串: qabaabbwerabtyababuiopasdfghjkabl,请写一段程序,找出重复出现的字符串(长度大于1的连续字符组合),以及他们出现的次数。通过控制台打印出来。
比如:
ab,6
aba,2
ba,2
思路:自己想的是先从整串中先取出2个字符,然后与剩下的字符串中取出2个字符逐一比较,显然我忽略了一些问题:遍历到已经对比过的字符串时重复计入了结果当中
自己需要注意的问题:考虑去除重复比对的字符串;分割比对的字符串是最大位置就是代码一中的(str.length-i),不用自己想想的那么麻烦;string中有IndexOf方法去查找包含字符的位置。
下面是同学写的一些方法,供参考:
代码一:
/************************************************* * 题目三:统计重复字符串 * *********************************************/using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace FindRepeatString{ class Program { static void Main(string[] args) { Console.WriteLine("请输入您要查找的字符串"); //string str = Console.ReadLine().Trim(); //string str = "qabaabbwerabtyababuiopasdfghjkabl"; string str = "abccabccab"; FindStringCount(str); Console.ReadKey(); } ////// 查找字符串中重复出现的字符串在字符串中出现的次数 /// /// 要查找的字符串 private static void FindStringCount(string str) { //判断截取的字符串是否已经查找过了 bool flagStr = false; //截取的字符串集合 ListstrList = new List (); //查找到的次数 int count = 0; //比较需要查找的字符个数是否大于1个 if (str.Length > 1) { for (int i = 2; i < str.Length; i++) //分割字符串,从2个开始,一直到最大 { //分割位置,从字符串第一个字符开始分割,一直到分割到最大字符-分割字符的个数 for (int n = 0; n < str.Length - i; n++) { string newstr = str.Substring(n, i); //分割需要比对的字符串 flagStr = false; //默认为没有判断过的 if (strList.Contains(newstr)) //判断字符串是否已经判断过了 { flagStr = true; } //如果字符串判断过了就不用判断,如果没有判断就开始查找重复出现了多少次 if (!flagStr) { strList.Add(newstr); //将字符串加入已经判断的字符串中去 for (int k = 0; k < str.Length; k++) //将提取的字符串与整串进行比对 { int indexOf = str.IndexOf(newstr, k); //如果有该字符串存在则返回位置,无返回-1 if (indexOf == -1) //判断是否有或者是否已经查找完毕了 { break; } //例如ab c ab cc ab ,先用ab比对得到第一ab位置,再 k = indexOf; count++; //记录出现次数加1 } //每个字符串肯定都会出现一次,是他本身,出现一次不打印 if (count > 1) { Console.WriteLine(newstr + "在字符串中出现了" + count + "次"); } //一个字符串判断玩了,字符串清0 count = 0; } } //分割不同个数的字符肯定不会一样,这里清空以查找的字符串,提高效率,很明显,2个字符和3个字符肯定不同 strList.RemoveRange(0, strList.Count); } } else { Console.WriteLine("请输入的字符串大于1个字符"); } } }}
代码二:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace FindRepeatString{ ////// 给定字符串,查找其中重复的子字符串积重复的次数 /// class Program { ////// 程序入口 /// /// static void Main(string[] args) { //Console.WriteLine("请输入字符串"); //string str = Console.ReadLine(); string str = "qabaabbwerabtyababuiopasdfghjkabl"; FindTheRepeatString(str); Console.ReadLine(); } ////// 查找字符串中重复子字符串出现的次数 /// /// 待查字符串 public static void FindTheRepeatString(string str) { //字符串起始位置 for (int start = 0; start < str.Length; start++) { //字符串的长度 for (int len = 2; len < str.Length - start + 1; len++) { //获取字符串 string pi = str.Substring(start,len); //判断该截取字符串是否已经查找过,如果查找过则不再重复查找 if (str.Substring(0,start).Contains(pi)) { continue; } //获取出现的次数 int count = CountNumber(str, pi); //输出出现次数大于1的子字符串 if (count > 1) { Console.WriteLine(pi + "重复出现" + count + "次"); } } } } ////// 统计字符串出现的次数 /// /// 原始字符串 /// 子字符串 ///public static int CountNumber(string str, string pi) { int count = 0; //子字符串的步长起始位置 int subStart = -1; while (true) { //每次从剩下的子字符串中去查找匹配字符串的索引位置,如果为-1,则没有查找到 int index = str.Substring(subStart + 1).IndexOf(pi); if (index == -1) { break; } count++; subStart += index + 1; } return count; } }}
代码三:
/********************************************************************************* * * 功能描述: 重复字符串计数 * * 作 者: yimeng * * 修改日期: 2013-8-18 * * 备 注: 有字符串:qabaabbwerabtyababuiopasdfghjkabl,请写一段程序,找出 * 重复出现的字符串(长度大于1的连续字符组合),以及他们出现的次数。 * 通过控制台打印出来。比如: * ab,6 * aba,2 * ba,2************************************************************************************/namespace MiddleExam{ using System; using System.Collections.Generic; ////// 重复字符串计数 /// class RepeatedString { ////// 给定的字符串 /// private const string myString = "qabaabbwffffffffffffhfhgfhgfyababuiopasdfghjkabl";//qabaabbwerabtyababuiopasdfghjkabl ////// 重复字符串计数 /// public static void GetRepeatedStringCount() { //首先把给定的字符串中,长度大于2的连续字符串找出来,放到一个集合里 ListstringList = GetContinueStringOfLengthGreaterThan2(myString); //遍历集合,对集合中每个字符串进行处理 foreach (string str in stringList) { int count = GetStringAppearCount(myString, str); //出现次数大于1的,打印出来 if (count > 1) { Console.WriteLine("{0},{1}", str, count); } } Console.ReadLine(); } /// /// 找出连续两个以上字符串,并放到一个集合中。 /// 注意:已经去掉重复的 /// /// 带处理的字符串 ////// 字符串集合 /// ////// 基本思路: /// 1.从位置0开始,截取长度为2的字符串,然后往前进一位,继续截取长度为2的字符串,以此类推,直到末尾。 /// 2.从位置0开始,截取长度为3的字符串,然后往前进一位,继续截取长度为3的字符串,以此类推,直到末尾。 /// 3.以此类推 /// private static ListGetContinueStringOfLengthGreaterThan2(string theString) { //定义一个空的字符串集合 List stringList = new List (); //截取字符串的长度:从2开始,依次递增。 for (int i = 2; i <= theString.Length; i++) { //截取的位置:从0开始,依次递增,但不能超过【总长度-截取的长度】。 for (int j = 0; j <= theString.Length - i; j++) { string str = theString.Substring(j, i);//开始截取 if (!stringList.Contains(str))//除重 { stringList.Add(str); } } } return stringList; } /// /// 获取一个指定的字符串在某个字符串中出现的次数 /// /// 源字符串 /// 给定字符串 ///出现的次数 ////// 基本思路: /// 查找theString出现的位置,找到后,继续往下找,每次找计数器都加1。 /// private static int GetStringAppearCount(string originalString, string theString) { int count = 0; //找到theString第一次出现的位置 int index = originalString.IndexOf(theString); /* * 如果theString出现过,则不断循环,从theString出现的位置的下一位开始再查找, * 看看是否还出现,以此类推,直到没有找到,则跳出循环 */ while (index >= 0) { //找到,则计数器加1 count++; //找到后,把originalString从【出现位置的下一位,总长度-出现位置-1】截取出来 originalString = originalString.Substring(index + 1, originalString.Length - index - 1); //进行下一次查找 index = originalString.IndexOf(theString); } return count; } }}