zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    npm配置国内源方法
    数据库—事务—隔离级别
    Mybatis—日志
    Mybatis—动态 SQL
    Mybatis—mapper.xml配置文件
    declare命令
    shell杂项
    流程控制语句
    第一篇博客
    Linux 命令[2]:mkdir
  • 原文地址:https://www.cnblogs.com/Ligeance/p/3538293.html
Copyright © 2011-2022 走看看