HNU Judging Troubles (字典树)

Judging Troubles
Time Limit: 5000ms, Special Time Limit:12500ms, Memory Limit:65536KB
Total submit users: 85, Accepted users: 63
Problem 13352 : No special judgement
Problem description

HNU Judging Troubles (字典树)_第1张图片
The NWERC organisers have decided that they want to improve the automatic grading of the submissions for the contest, so they now use two systems: DOMjudge and Kattis. Each submission is judged by both systems and the grading results are compared to make sure that the systems agree. However, something went wrong in setting up the connection between the systems, and now the jury only knows all results of both systems, but not which result belongs to which submission! You are therefore asked to help them figure out how many results could have been consistent.


Input

The input consists of:

• one line with one integer n (1 ≤ n ≤ 10^5), the number of submissions;

• n lines, each with a result of the judging by DOMjudge, in arbitrary order;

• n lines, each with a result of the judging by Kattis, in arbitrary order.

Each result is a string of length between 5 and 15 characters (inclusive) consisting of lowercase letters.


Output

Output one line with the maximum number of judging results that could have been the same for both systems.


Sample Input
5
correct
wronganswer
correct
correct
timelimit
wronganswer
correct
timelimit
correct
timelimit
Sample Output
4
Problem Source
NWERC 2014
#include<stdio.h>
struct node{
    node* nxt[26];
    int k;
    node(){
        for(int i=0; i<26; i++)
            nxt[i]=NULL;
        k=0;
    }
};
node* root;
void inset(char s[])
{
    node* p=root;
    int i=0;
    while(s[i]!='\0'){
        if(p->nxt[s[i]-'a']==NULL)
            p->nxt[s[i]-'a']=new node();
        p=p->nxt[s[i]-'a'];
        i++;
    }
    (p->k)++;
}
int query(char s[])
{
    node* p=root;
    int i=0;
    while(s[i]!='\0'&&p->nxt[s[i]-'a']!=NULL){
        p=p->nxt[s[i]-'a'];
        i++;
    }
    if(p->k&&s[i]=='\0')
    {
        (p->k)--; return 1;
    }
    return 0;
}
int main()
{
    int n;
    char s[20];
    while(scanf("%d",&n)>0)
    {
        root=new node();
        for(int i=0; i<n; i++)
        {
            scanf("%s",s);
            inset(s);
        }
        int ans=0;
        while(n--)
        {
            scanf("%s",s);
            ans+=query(s);
        }
        printf("%d\n",ans);
    }
}

你可能感兴趣的:(算法,字典树)