• 【luogu P2580 于是他错误的点名开始了】 题解


    题目链接:https://www.luogu.org/problemnew/show/P2580

    我真的永远都爱stl

    #include <map>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    const int maxn = 100001;
    map<string,int> a;
    int n, m, num[maxn];
    string s;
    int main()
    {
    	memset(num,-1,sizeof(num));
    	scanf("%d",&n);
    	for(int i = 1; i <= n; i++)
    	{
    		cin>>s;
    		a[s] = i;
    		num[a[s]]++;
    	}
    	scanf("%d",&m);
    	for(int i = 1; i <= m; i++)
    	{
    		cin>>s;
    		if(num[a[s]] ==-1)
    		{
    			printf("WRONG
    ");
    			continue;//记得continue一下,要不然重复点错名的话会出错
    		}
    		num[a[s]]++;
    		if(num[a[s]] == 1)
    		{
    			printf("OK
    ");
    		}
    		if(num[a[s]] > 1)
    		{
    			printf("REPEAT
    ");
    		}
    	}
    	return 0;
    }
    

    后来我发现,trie也很棒棒哦qaq

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    const int maxn = 500001;
    char s[50];
    int n, m, v[maxn], trie[maxn][26], opt, tot; 
    void insert()
    {
    	int len = strlen(s);
    	int root = 0;
    	for(int i = 0; i < len; i++)
    	{
    		int id = s[i] - 'a';
    		if(!trie[root][id])
    			trie[root][id]=++tot;
    		root = trie[root][id];	
    	}
    	v[root] = 1;
    }
    int find()
    {
    	int len = strlen(s);
    	int root = 0;
    	for(int i = 0; s[i]; i++)
    	{
    		int now = s[i] - 'a';
    		if(trie[root][now] == 0) return false;
    		root = trie[root][now];
    	}
    	if(v[root] == 1)
    	{
    		v[root] = 2;
    		return 1;
    	}
    	else
    	return 2;
    }
    int main()
    {
    	std::ios::sync_with_stdio(false);
    	cin>>n;
    	for(int i = 1; i <= n; i++)
    	{
    		cin>>s;
    		insert();
    	}
    	cin>>m;
    	for(int i = 1; i <= m; i++)
    	{
    		cin>>s;
    		opt = find();
    		if(opt == 0) printf("WRONG
    ");
    		if(opt == 1) printf("OK
    ");
    		if(opt == 2) printf("REPEAT
    ");
    	}
    }
    

    隐约雷鸣,阴霾天空,但盼风雨来,能留你在此。

    隐约雷鸣,阴霾天空,即使天无雨,我亦留此地。

  • 相关阅读:
    【leetcode】525. Contiguous Array
    【leetcode】827. Making A Large Island
    【leetcode】540. Single Element in a Sorted Array
    【leetcode】45. Jump Game II
    2017.11.2 Talk to customers for an hour
    New Concept English three (29)
    New Concept English three (28)
    2017.10.31 Enginer+position+statement
    New Concept English three (27)
    2017.10.30 Epicor -ERP
  • 原文地址:https://www.cnblogs.com/MisakaAzusa/p/9216212.html
Copyright © 2020-2023  润新知