zoukankan      html  css  js  c++  java
  • 程序设计思维与实践 Week15 作业 (3/4/数据班)

    程序设计思维与实践 Week15 作业 (3/4/数据班)

    A - ZJM 与霍格沃兹(必做)

    问题分析

    将魔咒与对应功能保存到数组中,录字符串哈希值与数组下标,对于要查询的字符串,求取其哈希值并在map中查找。

    #include <iostream>
    #include <string.h>
    #include <map>
    using namespace std;
    const int seed=7; 
    char str1[100005][25];
    char str2[100005][85];
    map<unsigned long long,int> mp;
    unsigned long long Hash(char*str,int l,int r)
    {
    	unsigned long long ans=0,temp=seed;
    	for(int i=l;i<=r;i++)
    	{
    		if(str[i]!=' ')
    		{
    			ans+=str[i]*temp;
    			temp*=temp;
    		}
    	}
    	return ans;
    }
    int main() {
    	int n=0;
    	char temp[105];
    	while(scanf("%[^
    ]",temp)&&strcmp(temp,"@END@")!=0)
    	{
    		getchar();
    		int len=strlen(temp);
    		int l=1,r=1;
    		for(;r<len&&temp[r]!=']';r++);
    		strncpy(str1[n],temp+1,r-l);
    		unsigned long long h=Hash(temp,l,r-1);
    		mp[h]=n;
    		l=r+2;r=len;
    		strncpy(str2[n],temp+l,r-l);
    		h=Hash(temp,l,r-1);
    		mp[h]=n;
    		n++;
    	}
    	int num;
    	scanf("%d",&num);getchar();
    	for(int i=0;i<num;i++)
    	{
    		scanf("%[^
    ]",temp);getchar();
    		int len=strlen(temp);
    		unsigned long long h;
    		if(temp[0]=='[')
    			h=Hash(temp,1,len-2);
    		else
    			h=Hash(temp,0,len-1);
    		if(mp.find(h)==mp.end())
    			printf("what?
    ");
    		else
    		{
    			if(temp[0]=='[')
    				printf("%s
    ",str2[mp[h]]);
    			else
    				printf("%s
    ",str1[mp[h]]);
    		}
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    3.1《想成为黑客,不知道这些命令行可不行》(Learn Enough Command Line to Be Dangerous)——下载文件
    rem实现手机页面自动缩放
    Git 常用命令
    使用 canvas+JS绘制钟表
    JS 操作数组的方法
    Node.js Request方法
    兼容浏览器的点击事件
    ES6知识点
    上传项目到github上
    JavaScript 编码风格
  • 原文地址:https://www.cnblogs.com/master-cn/p/13052606.html
Copyright © 2011-2022 走看看