zstu-2437 English word

2437: English word

Time Limit: 1 Sec  Memory Limit: 64 MB
Submit: 2580  Solved: 1174

Description

You still are worried about reading acm English problem, let me tell you a kind of very good method of Memorising Words, the root memory method,.the most interesting of the method is the prefix root. Now there are some English words to remembeand he knows some prefix root;He want to know how many words that can be remembered using each the prefix .in other words,how many words that contain the prefix ,Now to acmer you for help, can you help him?

Input

The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer number N that indicates the number of words (1 <= N <= 10000). Then exactly N lines follow, each containing a single word. Each word contains at least two and at most 10 lowercase characters, that means only letters ‘a‘ through ‘z‘ will appear in the word. The same word may appear several times in the list. Then follow a line containing a single integer number M that indicates the number of Words prefix question (0 <= M<= 10000). Then exactly M lines follow, each containing a single Words prefix.

Output

For the given input data, output exact M line, each line contain a ingle integer indicates the answer of each query.

Sample Input

1
4
preview
predict
premier
press
3
pre
press
pree

Sample Output

4
1
0

HINT

Source

style_luo

用来练一下字典树模板,当年用快排+二分做的也很开心

#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef struct node {
    int num;
    node *next[30];
    node() {
        memset(next, 0, sizeof(next));
        num = 0;
    }
}Trie;
char op[32], s[32];
void Insert(node *root, char *s) {
    node *p = root;
    for(int i = 0; s[i]; i++) {
        int x = s[i] - ‘a‘;
        if(p -> next[x] == NULL) p -> next[x] = new node;
        p = p -> next[x];
        p -> num++;
    }
}
int Search(node *root, char *s) {
    node *p = root;
    for(int i = 0; s[i]; i++) {
        int x = s[i] - ‘a‘;
        if(p -> next[x] == NULL) return 0;
        p = p -> next[x];
    }
    return p -> num;
}

void Delete(node *root, char *s, int cnt) {
    node *p = root;
    for(int i = 0; s[i]; i++) {
        int x = s[i] - ‘a‘;
        p = p -> next[x];
        p -> num -= cnt;
    }
    for(int i = 0; i < 30; i++) p -> next[i] = 0;
}

int main() {
    int t;
    scanf("%d", &t);
    while(t--) {
        Trie *root = new node;
        int n;
        scanf("%d", &n);
        for(int i = 1; i <= n; i++) {
            scanf("%s", s);
            Insert(root, s);
        }
        scanf("%d", &n);
        for(int i = 1; i <= n; i++) {
            scanf("%s", s);
            printf("%d\n", Search(root, s));
        }
    }
}
#include<bits/stdc++.h>
using namespace std;
string ch[11111];
string pre[11111];
int cnt=0;

void jiancha_right(int val2,string pr2)
{
    if(val2 >= 0 && ch[val2].compare(0, pr2.size(), pr2) == 0)
    {
        cnt++;
        jiancha_right(val2+1,pr2);
    }
    else return;
}

void jiancha_left(int val2,string pr2)
{
    if(val2 >= 0 && ch[val2].compare(0, pr2.size(), pr2) == 0)
    {
        cnt++;
        jiancha_left(val2-1,pr2);
    }
    else return;
}

int bbsearch(string pr,int m1,int n1)
{
    int val;
    while(m1<n1)
    {
        val=m1+(n1-m1)/2;
        //printf("%s %s %d %d %d\n",ch[val],pr,m1,n1,val);
        if(ch[val].compare(0, pr.size(), pr)==0)
        {
            cnt++;
            jiancha_left(val-1,pr);
            jiancha_right(val+1,pr);
            break;
        }
        else if(pr < ch[val]) n1=val;
        else m1=val+1;
    }
    return cnt;
}

int main()
{
    int t,m,n;
    char s[1];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            cin >> ch[i];
        }
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            cin >> pre[i];
        }

        sort(ch, ch + m);
        int ans;

        for(int i=0;i<n;i++)
        {
            cnt=0;
            ans=bbsearch(pre[i],0,m);
            printf("%d\n",ans);
        }

    }
}
时间: 2024-08-06 03:04:43

zstu-2437 English word的相关文章

[LeetCode 273] Integer to English Word

Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. For example, 123 -> "One Hundred Twenty Three" 12345 -> "Twelve Thousand Three Hundred Forty Five" 1234567 -&g

Integer to English word leetcode java

问题描述: Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. For example, 123 -> "One Hundred Twenty Three" 12345 -> "Twelve Thousand Three Hundred Forty Five" 12345

LeetCode691. Stickers to Spell Word

We are given N different types of stickers. Each sticker has a lowercase English word on it. You would like to spell out the given target string by cutting individual letters from your collection of stickers and rearranging them. You can use each sti

691. Stickers to Spell Word

We are given N different types of stickers. Each sticker has a lowercase English word on it. You would like to spell out the given target string by cutting individual letters from your collection of stickers and rearranging them. You can use each sti

POJ 2503 Babelfish (Trie树 或 map)

Babelfish Time Limit: 3000MS        Memory Limit: 65536K Total Submissions: 34278        Accepted: 14706 Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately

【转载】Discriminative Learning和Generative Learning

Discriminative Learning和Generative Learning 2011-12-08 10:47 1929人阅读 评论(2) 收藏 举报 variablesdependencies算法includeparametersexpress Discriminative 学习算法是一类模型化输入(X)输出(Y)的关系的方法,简单来说就好比中医,我们只知道用若干个药(当归,虎骨...)可以凑成一个药方,就能治疗跌打病痛.但我们并不去了解内部的原因,我们将其看做一个黑盒,只需了解X和

POJ2503——Babelfish(map映射+string字符串)

Babelfish DescriptionYou have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.InputInput consists of up to 100,000 diction

POJ2503-Babelfish-二分

Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 44545   Accepted: 18803 Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have

Kattis - Babelfish

Babelfish You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them. Input Input consists of up to 100000100000 dictionary