HDU2222 Keywords Search
来源:互联网 发布:网络报警平台网络诈骗 编辑:程序博客网 时间:2024/06/03 19:17
Problem Description
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.
Input
First line will contain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.
Output
Print how many keywords are contained in the description.
Sample Input
15shehesayshrheryasherhs
Sample Output
3
代码如下:
import java.util.ArrayList;import java.util.LinkedList;import java.util.Queue;import java.util.Scanner;class Node{Node[] childs;Node fail;int sum;public Node() {sum=0;childs=new Node[26];}}public class Main {public Node insert(Node root,String s){Node temp=root;root.sum++;for(int i=0;i<s.length();i++){int index=s.charAt(i)-'a';if(temp.childs[index]==null){temp.childs[index]=new Node();}temp=temp.childs[index];temp.fail=root;}temp.sum++;return root;}public void getFail(Node root){Queue<Node> queue=new LinkedList<Node>();queue.add(root);while(!queue.isEmpty()){Node node=queue.poll();for(int i=0;i<26;i++){if(node.childs[i]!=null){//if(node==root) node.childs[i].fail=root;//else{Node temp=node.fail;while(temp!=null){if(temp.childs[i]!=null){node.childs[i].fail=temp.childs[i];break;}temp=temp.fail;}if(temp==null) node.childs[i].fail=root;//}queue.add(node.childs[i]);}}}}public int query(String s,Node root){Node temp=root;int res=0;for(int i=0;i<s.length();i++){int index=s.charAt(i)-'a';while(temp.childs[index]==null&&temp!=root) temp=temp.fail;temp=temp.childs[index];if(temp==null) temp=root;Node p=temp;while(p!=root&&p.sum!=0){res+=p.sum;p.sum=0;p=p.fail;}}return res;}public static void main(String[] args) {Scanner scan=new Scanner(System.in);Main main=new Main();int caseNum=scan.nextInt();ArrayList<Integer> list=new ArrayList<Integer>();for(int k=0;k<caseNum;k++){int num=scan.nextInt();Node root=new Node();for(int i=0;i<num;i++){String str=scan.next();main.insert(root, str);}//main.print(root);main.getFail(root);String text=scan.next();list.add(main.query(text, root));}for(Integer a:list) System.out.println(a);}}
阅读全文
0 0
- HDU2222 Keywords Search
- hdu2222 Keywords Search
- hdu2222 Keywords Search
- hdu2222 Keywords Search
- HDU2222--Keywords Search
- hdu2222 Keywords Search
- 【hdu2222】Keywords Search
- HDU2222 Keywords Search
- hdu2222 Keywords Search
- HDU2222---Keywords Search
- HDU2222 Keywords Search
- hdu2222(Keywords Search)
- hdu2222 Keywords Search
- HDU2222 Keywords Search
- hdu2222 Keywords Search
- hdu2222 Keywords Search
- hdu2222 Keywords Search
- 【HDU2222】Keywords Search
- 欧几里德算法GCD
- jVM加载class文件的原理机制
- 菜鸟教程例题---第一题:【1,2,3,4】组成无重复的3位数
- 结合内核实现源码分析 select poll epoll区别
- Spring Data JPA实现分页Pageable的实例代码
- HDU2222 Keywords Search
- shiro安全框架
- hdu 3944 DP?
- 接口测试方案(Rest类型接口)
- Android中BitmapFactory.Options详解
- 华为OJ——将真分数分解为埃及分数
- eclipse--maven项目没有自动编译的问题
- UESTC
- 【Linux】top命令详解——进程管理器