Search III

Your task is to write a program of a simple dictionary which implements the following instructions:

  • insert str: insert a string str in to the dictionary
  • find str: if the distionary contains str, then print 'yes', otherwise print 'no'

Input

In the first line n, the number of instructions is given. In the following n lines, n instructions are given in the above mentioned format.

Output

Print yes or no for each find instruction in a line.

Constraints

  • A string consists of 'A', 'C', 'G', or 'T'
  • 1 ≤ length of a string ≤ 12
  • n ≤ 1000000

Sample Input 1

5
insert A
insert T
insert C
find G
find A

Sample Output 1

no
yes

Sample Input 2

13
insert AAA
insert AAC
insert AGA
insert AGG
insert TTT
find AAA
find CCC
find CCC
insert CCC
find CCC
insert T
find TTT
find T

Sample Output 2

yes
no
no
yes
yes
yes 做这道题用散列查找,但是我写的第一个开放地址法,显示超时,于是我又用链地址进行求解,最后通过AC,毕竟链地址对于冲突的出现非常少,但是也因为链地址的实现比较复杂容易出错
(毕竟是使用指针来实现的),所以首选不会使用链地址,但不可否认的是链地址的确效率很高。
链地址AC代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<malloc.h>
using namespace std;
const int maxn=1e6+10;
const int MAX=999983;
long long int zhuanhuan(char s[]);
struct node
{
long long int b;
struct node *p;
}a[maxn];
int main()
{
int n;
for(int i=0;i<maxn;i++)
a[maxn].b=0,a[maxn].p=NULL;
char str1[10],str2[20];
cin>>n;
while(n--)
{
scanf("%s%s",str1,str2);
long long int u=zhuanhuan(str2);
int m=u%MAX;
if(strcmp(str1,"insert")==0)
{
struct node *next=a[m].p;
if(a[m].b==0) a[m].b=u,a[m].p=(struct node *)malloc(sizeof(struct node)),a[m].p->b=0,a[m].p->p=NULL;
else
{
while(1)
{

if(next->b==0)
{
next->b=u;
next->p=(struct node *)malloc(sizeof(struct node));
next->p->b=0;
next->p->p=NULL;
break;
}
else next=next->p;
}
}
}
else
{
if(a[m].b==u) cout<<"yes"<<endl;
else
{
if(a[m].p==NULL) cout<<"no"<<endl;
else
{
struct node *next=a[m].p;
while(1)
{
if(next->b==u) {cout<<"yes"<<endl;break;}
else
{
if(next->p==NULL)
{
cout<<"no"<<endl;
break;
}
else next=next->p;
}
}
}
}
}
}
return 0;
}

long long int zhuanhuan(char s[])
{
long long int sum=0;
for(int i=0;s[i]!='\0';i++)
{
switch(s[i])
{
case 'A':sum+=1*pow(10,i);break;
case 'G':sum+=2*pow(10,i);break;
case 'C':sum+=3*pow(10,i);break;
case 'T':sum+=4*pow(10,i);break;
}
}
return sum;
}

到了第二天,还是不死心,仍旧想除链地址外的其他方法做出,因为我认为只要散列函数写得好,应该可以做出,可以不超时。但即便用再散列函数法,依旧还不行。但皇天不负有心人,最终让我AC了,但是在第一天的基础上仅仅改了一个地方,让我非常的无奈,为什么一开始就没有想到,现附上AC代码,并在改的地方加上注释:

#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std;
const int maxn=1e6+10;
const int MAX=999983;
long long int zhuanhua(char s[]);
int main()
{
long long int a[maxn],n;
char str1[10],str2[15];
cin>>n;
while(n--)
{
scanf("%s%s",str1,str2);
long long int sum=zhuanhua(str2);
int m=sum%MAX;
int s=0;
if(str1[0]=='i')
{
int i=0;
do
{
if(a[(m+i)%MAX]==0)
{
a[(m+i)%MAX]=sum;
break;
}
if(a[(m+i)%MAX]==sum) break; //这句话在insert中遇到极端情况(有许多insert都是一样的时候)可以很大效率上减少冲突
} while(++i);
if(i>s) s=i;
}
else
{
int i=0;
do
{
if(a[(m+i)%MAX]==0)
{
cout<<"no"<<endl;
break;
}
if(a[(m+i)%MAX]==sum)
{
cout<<"yes"<<endl;
break;
}
}while(++i);
}
}
return 0;
}

long long int zhuanhua(char s[])
{
long long int sum=0;
for(int i=0;s[i]!='\0';i++)
{
switch(s[i])
{
case 'A':sum+=1*pow(5,i);break;                             //我本人认为这里的算法应该是使得对应的值唯一,可是我自己没有将其进行数理逻辑证明
case 'G':sum+=2*pow(5,i);break;
case 'C':sum+=3*pow(5,i);break;
case 'T':sum+=4*pow(5,i);break;
}
}
//cout<<sum<<endl;
return sum;
}

散列法属于一种搜索 存储的算法,利用个元素的值确定存储位置,于是也会利用个元素的值进行搜索。

Aizu - ALDS1_4_C Dictionary的更多相关文章

  1. C#数组,List,Dictionary的相互转换

    本篇文章会向大家实例讲述以下内容: 将数组转换为List 将List转换为数组 将数组转换为Dictionary 将Dictionary 转换为数组 将List转换为Dictionary 将Dicti ...

  2. ASP.NET Aries JSAPI 文档说明:AR.DataGrid、AR.Dictionary

    AR.Global 文档 1:对象或属性: 名称 类型 说明 DG 对象 DataGrid操作对象 //datagrid集合,根据ID取出DataGrid对象,将Json当数组用. Items: ne ...

  3. WebAPI接口返回ArrayList包含Dictionary对象正确解析

    一.问题提出 为了减少流量,将key-value(键值对)直接输出到Dictionary<string, string>,接口返回结果如下: 其中{}里面内容如下: 上图显示600是键,4 ...

  4. Linq在Array,List,Dictionary中的应用

    Linq在Array,List,Dictionary中的应用 今天在实际工作中需要对array,list,dictionary进行排序,试一试linq,发现非常好用,代码如下: using Syste ...

  5. python之最强王者(8)——字典(dictionary)

    1.Python 字典(Dictionary) 字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包 ...

  6. Swift3 - String 字符串、Array 数组、Dictionary 字典的使用

    Swift相关知识,本随笔为 字符串.数组.字典的简单使用,有理解.使用错误的地方望能指正. ///************************************************** ...

  7. [LeetCode] Alien Dictionary 另类字典

    There is a new alien language which uses the latin alphabet. However, the order among letters are un ...

  8. Dictionary

    命名空间:System.Collections.Generic(程序集:mscorlib) Dictionary<TKey, TValue> 类   一般用法:通过key获取value,k ...

  9. 关于 Dictionary<string,string>,和List<T>在View的使用

    在MVC中Dictionary<string,string>如何应用到View页面中呢,例: <input type="text" name=key value= ...

随机推荐

  1. SCUT - 153 - 小马哥和他的山脉 - 线段树

    https://scut.online/p/153 其实不需要用线段树,只关心相邻元素的差,像神仙那样用差分就可以O1维护的. 但是我偏要用. 交之前写的那个,注意没有st本身的线段树只有lazy标记 ...

  2. win10专业版Hyper-v下Docker挂载volume的方式使用Gitlab(汉化版)保存资料数据(使用外部redis)

    目录 话题 (191) 笔记 (137) 资料区 (2) 评价 (33) 介绍 讨论区 话题 win10专业版Hyper-v下Docker挂载volume的方式使用Gitlab(汉化版)保存资料数据( ...

  3. Linux常用指令全集

    Linux简介及Ubuntu安装 常见指令 系统管理命令 打包压缩相关命令 关机/重启机器 Linux管道 Linux软件包管理 vim使用 用户及用户组管理 文件权限管理 大牛笔记-www.weix ...

  4. Malware分析

    //文章来源:http://www.2cto.com/Article/201312/265217.html by Kungen@CyberSword 想要查找恶意样本,首先要知道查找样本所需的基本信息 ...

  5. spark复习笔记(7):sparkSQL

    一.saprkSQL模块,使用类sql的方式访问Hadoop,实现mr计算,底层使用的是rdd 1.hive //hadoop  mr  sql 2.phenoix //hbase上构建sql的交互过 ...

  6. 利用JFreeChart生成简单柱状图(Java)

    package barchartdemo1; import <a href="http://lib.csdn.net/base/javaee" class='replace_ ...

  7. Spark2.0基于广播变量broadcast实现实时数据按天统计

    package com.gm.hive.SparkHive; import java.text.SimpleDateFormat; import java.util.Arrays; import ja ...

  8. 采集容器内存并写到excel

    # coding=utf-8 import os import commands import re from pyExcelerator import * def execute(cmd): sta ...

  9. RabbitMQ走过的坑,发送的消息是乱码

    发送的消息在可视化界面中是乱码,如图: 看见这个content_tpye没有,是不是很奇怪,就是这个坑,设置下就行,看代码: @Bean Jackson2JsonMessageConverter me ...

  10. Task9.Attention

    注意力模型最近几年在深度学习各个领域被广泛使用,无论是图像处理.语音识别还是自然语言处理的各种不同类型的任务中,都很容易遇到注意力模型的身影.所以,了解注意力机制的工作原理对于关注深度学习技术发展的技 ...