zoukankan      html  css  js  c++  java
  • HRBUST 1987 逃课的孩子

    Sol:HASH + 二分  字符串处理,很基础的操作。

    题意很明确就是找重复的次数统计下,范围比较大1n10000,1m10000。


    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    const int maxm = 10000 + 10;
    const int maxn = 10 + 10;
    const int EXP = 52;
    
    int n,m;
    long long list[maxm];
    char str[maxn];
    
    long long hash(char *str,int len)
    {
    	long long res=0;
    	long long tmp;
    	for(int i=0;i<len;i++)
    	{
    		if(str[i]>='A'&&str[i]<='Z')
    			tmp=str[i]-'A';
    		if(str[i]>='a'&&str[i]<='z')
    			tmp=str[i]-'a'+26;
    		res=res*EXP+tmp;
    	}
    	return res;
    }
    
    bool Bin_search(int L,int R,long long val)
    {
    	int mid;
    	while(L<=R)
    	{
    		mid=(L+R)>>1;//  / 2
    		if(list[mid]==val) return true;
    		if(list[mid]>val) R=mid-1;
    		else L=mid+1;
    	}
    	return false;
    }
    
    int main()
    {
    	while(~scanf("%d%d",&n,&m))
    	{	
    		int cnt=1;
    		list[0]=-1;
    		for(int i=1;i<=n;i++)
    		{
    			scanf("%s",str);
    			int len1=strlen(str);
    			list[cnt++]=hash(str,len1);
    		}
    		sort(list,list+n+1);
    		for(int i=1;i<=m;i++)
    		{
    			scanf("%s",str);
    			int len2=strlen(str);
    			if(Bin_search(1,cnt,hash(str,len2))) 
    				printf("yes
    ");
    			else 
    				printf("no
    ");
    		}
    	}
    	return 0;
    }


  • 相关阅读:
    LeetCode
    LeetCode
    LeetCode OJ
    LeetCode OJ
    LeetCode OJ
    关于if和else和switch case的用法和程序编码操作过程
    关于java的特点
    关于JAVA的数据类型
    关于java的学习
    力扣482. 密钥字符串 S python--每天一题
  • 原文地址:https://www.cnblogs.com/fuhaots2009/p/3469017.html
Copyright © 2011-2022 走看看