15. 字符串匹配

有一个长度为n的字符串str,有非常多的关键字query(长度不超过10),需要判断每个关键字是否是str的子串。

注意:query是动态的输入进行查询的,预先并不知道所有的query

请实现2个函数initWithString(str)existSubString(query)。我们会首先调用一次initWithString(str),你可以在这个函数中做一些预处理操作。然后对于每一个query,函数existSubString(query)需要返回这个query是否为str的子串。

 

感觉理解题目有问题,总之,先写下来练习吧。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication12
{
    class Program
    {
        static void Main(string[] args)
        {
            SubString subStr = new SubString();
            subStr.InitWithString("I am a Teacher.");
            bool result = subStr.ExistSubString("am");
            Console.WriteLine(result);
        }
    }

    class SubString
    {
        Hashtable ht = new Hashtable();

        public void InitWithString(string str)
        {
            string[] strArr = str.Split(' ', ',', '.');
            for (int i = 0; i < strArr.Length; i++)
            {
                ht[strArr[i]] = true;
            }
        }
        public bool ExistSubString(string query)
        {
            if (ht[query] !=null)
            {
                return true;
            }
            return false;
        }
    }
}
View Code

 

posted @ 2014-02-05 15:32  Ligeance  阅读(195)  评论(0编辑  收藏  举报