魔鬼中 2018-10-28 05:53 采纳率: 81.8%
浏览 1082
已采纳

C#控制台程序,怎么查找重复子串?

编写控制台应用程序,判断用户输入的字符串中是否存在重复的由两个小写字母组成的子串,如果有则按从左到右的出现顺序输出重复出现的子串及次数;如果用户输入的字符串中包含非小写字母则输出“lnputting illegal characters”。

输入格式:

输入字符串

输出格式:

每行输出一个重复出现的子串及出现的次数,子串与次数之间用一个空格隔开。

例如

输入:

aaabbaabb

输出:

aa 2

ab 2

ba 2

希望大佬能给解释详细点,谢谢!

  • 写回答

2条回答

  • threenewbee 2018-10-28 06:21
    关注
     using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    
    namespace Q704577
    {
        class Program
        {
            static void Main(string[] args)
            {
                string input = Console.ReadLine();
                if (!input.All(x => x >= 'a' && x <= 'z'))
                {
                    Console.WriteLine("lnputting illegal characters");
                    return;
                }
                var lookups = Enumerable.Range(0, input.Length - 3).Select(x => input.Substring(x, 2)).Distinct();
                var query = lookups.Select(x => new { a = x, b = Regex.Matches(input, x).Count }).Where(x => x.b >= 2);
                foreach (var item in query)
                {
                    Console.WriteLine("{0} {1}", item.a, item.b);
                }
            }
        }
    }
    
    

    图片说明

    如果问题得到解决,请点我回答右边的采纳,谢谢

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料