Add and Search Word

Design a data structure that supports the following two operations:

void addWord(word)

bool search(word)

search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.

样例

addWord("bad")

addWord("dad")

addWord("mad")

search("pad") -> false

search("bad") -> true

search(".ad") -> true

search("b..") -> true

注意

You may assume that all words are consist of lowercase letters a-z.

class Node
{
public:
	Node *child[26];
	bool isEnd;
	char val;
	Node(char a)
	{
		val = a;
		for (int i = 0; i < 26; i++)
		{
			child[i] = NULL;
		}
		isEnd = false;
	}
};

class WordDictionary {
public:
    WordDictionary()
    {
        root = new Node(' ');
    }
    // Adds a word into the data structure.
    void addWord(string word) {
        // Write your code here
        addWordHelp(root, word);
    }

    // Returns if the word is in the data structure. A word could
    // contain the dot character '.' to represent any one letter.
    bool search(string word) {
        // Write your code here
        return searchHelp(root, word);
    }
private:
    void addWordHelp(Node *root, string word)
    {
	    if (root == NULL)
	    {
		    return;
	    }

	    int n = word.length();
	    if (n < 1)
	    {
		    return;
	    }
        int pos = word[0] - 'a';
	    if (root->child[pos] == NULL)
	    {
		    root->child[pos] = new Node(word[0]);
	    }
	    if (n == 1)
	    {
		    root->child[pos]->isEnd = true;
	    }
	    else
	    {
		    addWordHelp(root->child[pos], word.substr(1));
	    }
    }
    
    bool searchHelp(Node *root, string word)
    {
	    if (root == NULL)
	    {
		    return false;
	    }

	    int n = word.length();
	    if (n < 1)
	    {
		    return true;
	    }
	    if (word[0] == '.')
	    {
		    for (int i = 0; i < 26; i++)
		    {
			    if (root->child[i] != NULL)
			    {
				    if (n == 1)
				    {
					    if (root->child[i]->isEnd)
					    {
						    return true;
					    }
				    }
				    else
				    {
					    if (searchHelp(root->child[i], word.substr(1)))
					    {
						    return true;
					    }
				    }
			    }
		    }
		    return false;
	    }
	    else
	    {
		    int pos = word[0] - 'a';
		    if (root->child[pos] != NULL)
		    {
			    if (n == 1)
			    {
				    if (root->child[pos]->isEnd)
				    {
					    return true;
				    }
				    else
				    {
					    return false;
				    }
			    }
			    else
			    {
				    return searchHelp(root->child[pos], word.substr(1)); 
			    }
		    }
		    else
		    {
			    return false;
		    }
	    }
    }
    Node *root;
};

// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary;
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");


以下是修改后的WebCrawler.java代码,请仔细查看注释: ```java import java.util.*; import java.net.*; import java.io.*; public class WebCrawler { public static void main(String[] args) { Scanner input = new Scanner(System.in); //从用户输入中获取起始URL和要搜索的关键字 System.out.print("Enter the starting URL: "); String startingURL = input.nextLine(); System.out.print("Enter the keyword to search for: "); String keyword = input.nextLine(); //创建一个HashSet来存储已访问的URL HashSet<String> urls = new HashSet<>(); //将起始URL添加到HashSet中 urls.add(startingURL); //不断从HashSet中获取URL并解析它们 while (!urls.isEmpty()) { String urlString = urls.iterator().next(); urls.remove(urlString); //如果URL以"http"开头,则继续解析 if (!urlString.startsWith("http")) continue; //如果该URL已经被访问,则跳过 if (urls.contains(urlString)) continue; //将该URL添加到已访问的URL集合中 urls.add(urlString); try { URL url = new URL(urlString); Scanner urlInput = new Scanner(url.openStream()); //读取该URL的内容,并在其中查找关键字 while (urlInput.hasNext()) { String line = urlInput.nextLine(); if (line.contains(keyword)) { System.out.println("Keyword found at " + urlString); return; } } } catch (Exception ex) { System.out.println("Error: " + ex.getMessage()); } } System.out.println("Keyword not found."); } } ``` 请注意,该程序会从用户输入中获取起始URL和要搜索的关键字,并不断地从已访问的URL列表中获取URL并解析它们,直到找到包含关键字的页面或没有更多的未访问的URL为止。如果找到了包含关键字的页面,则程序会打印该页面的URL并终止。如果没有找到包含关键字的页面,则程序会打印一条消息并终止。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值