模式匹配

有模式 *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

 1None.gifusing System;
 2None.gifusing System.Text;
 3None.gifusing System.IO;
 4None.gif
 5None.gifclass Class1
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 8InBlock.gif    /// 应用程序的主入口点。
 9ExpandedSubBlockEnd.gif    /// </summary>

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

47InBlock.gif
48ExpandedSubBlockEnd.gif                }

49ExpandedSubBlockEnd.gif            }

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

58InBlock.gif
59InBlock.gif
60ExpandedSubBlockEnd.gif    }

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

68InBlock.gif        return sb.ToString();
69ExpandedSubBlockEnd.gif    }

70InBlock.gif                    
71ExpandedBlockEnd.gif}

72None.gif

转载于:https://www.cnblogs.com/xiexiaokui/archive/2005/06/04/168035.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值