zoukankan      html  css  js  c++  java
  • 模式匹配

    有模式 *A*A'* ,其中*表示任意子字符串(可以是空串),A也表示任意子字符串(也可以是空串),A'是A的逆序,例如假设A是23ad4那么A'就是4da32。

     这样,任意字符串都符合这个模式,而且会有多个匹配方法,例如字符串

     13412432145

     可以匹配为 1 34 1 2432145 其中A=1
    也可以匹配为 1 34 12 43 2145 其中A=34
    也可以匹配为 13 412 43 214 5 其中A=412

     问题:写一个程序,输入任意字符串X=*A*A'*,求长度最大的A

     例如输入 13412432145 则最大长度A=412,长度等于3

     1using System;
     2using System.Text;
     3using System.IO;
     4
     5class Class1
     6{
     7    /// <summary>
     8    /// 应用程序的主入口点。
     9    /// </summary>

    10    [STAThread]
    11    static void Main(string[] args)
    12    {
    13    
    14        int count=0;
    15        Console.WriteLine("Input the test string:");
    16        string s = Console.ReadLine();
    17        bool found = false;
    18        while(s!=null && s!="")
    19        {
    20            int n = s.Length;
    21            for(int m=n/2;m>0;m--)
    22            {
    23                for(int i=0;i<n-m;i++)
    24                {
    25                    string a = s.Substring(i,m);
    26                    string a_ = Reverse(a);
    27                    int index = s.IndexOf(a_,i+m);
    28                    count++;
    29                    if(index!=-1)
    30                    {
    31                        Console.WriteLine("比较次数:"+count);
    32                        Console.WriteLine("A= "+a);
    33                        Console.WriteLine("模式为");
    34                        if(i-1>0)
    35                        Console.Write(s.Substring(0,i-1)+" ");
    36                        Console.Write(a+" ");
    37                        if(index-i-m>0)
    38                        Console.Write(s.Substring(i+m,index-i-m)+ " ");
    39                        Console.Write(a_+ " ");
    40                        
    41                        Console.WriteLine(s.Substring(index+m));
    42                        found = true;
    43
    44                        goto label;
    45
    46                    }

    47
    48                }

    49            }

    50        label:
    51            if(found == false)
    52                Console.WriteLine(@"a=""");
    53
    54            Console.WriteLine("Inpu the test string:");
    55            s = Console.ReadLine();
    56
    57        }

    58
    59
    60    }

    61    static string Reverse(string s)
    62    {
    63        StringBuilder sb = new StringBuilder();
    64        for(int i=s.Length-1;i>=0;i--)
    65        {
    66            sb.Append(s[i]);
    67        }

    68        return sb.ToString();
    69    }

    70                    
    71}

    72
  • 相关阅读:
    多线程中lock用法
    跨域使用Proxy page或Cross Frame
    new 操作符所做的事情
    跨域使用js文件
    跨域使用window.name
    TFS 映射错误( is already mapped in workspace)解决办法
    跨域使用JSONP
    jquery加载页面中的部分内容
    CQRS架构案例Tiny Library CQRS详解:AOP拦截与异常处理
    面向领域驱动架构的查询实现方式
  • 原文地址:https://www.cnblogs.com/xiexiaokui/p/168035.html
Copyright © 2011-2022 走看看