之前就听说过有个叫做hash表的东西,这段时间在上信息论与编码,也接触了一些关于编码的概念,直到今天做百度之星的初赛的d题时,才第一次开始学并用hash

  一开始我用的是mutimap和mutiset,先对字符串从小到大排序,再存进mutimap中,之后遍历mutimap的键,结果都超时了,代码如下:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define in(n) scanf("%d",&(n))
#define in2(x1,x2) scanf("%d%d",&(x1),&(x2))
#define inll(n) scanf("%I64d",&(n))
#define inll2(x1,x2) scanf("%I64d%I64d",&(x1),&(x2))
#define inlld(n) scanf("%lld",&(n))
#define inlld2(x1,x2) scanf("%lld%lld",&(x1),&(x2))
#define inf(n) scanf("%f",&(n))
#define inf2(x1,x2) scanf("%f%f",&(x1),&(x2))
#define inlf(n) scanf("%lf",&(n))
#define inlf2(x1,x2) scanf("%lf%lf",&(x1),&(x2))
#define inc(str) scanf("%c",&(str))
#define ins(str) scanf("%s",(str))
#define out(x) printf("%d\n",(x))
#define out2(x1,x2) printf("%d %d\n",(x1),(x2))
#define outf(x) printf("%f\n",(x))
#define outlf(x) printf("%lf\n",(x))
#define outlf2(x1,x2) printf("%lf %lf\n",(x1),(x2));
#define outll(x) printf("%I64d\n",(x))
#define outlld(x) printf("%lld\n",(x))
#define outc(str) printf("%c\n",(str))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define mem(X,Y) memset(X,Y,sizeof(X));
typedef vector<int> vec;
typedef long long ll;
typedef pair<int,int> P;
const int dx[]={,,-,},dy[]={,,,-};
const int INF=0x3f3f3f3f;
const ll mod=1e9+;
ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
const bool AC=true; int n,ans;
string str;
/*int main(){
in(n);
multimap <string, int> mp;
rep(i,0,n){
cin>>str;
sort(str.begin(),str.end());
ans=0;
multimap <string, int>:: iterator it;
for(it=mp.begin(); it != mp.end(); it++) {
if(string((*it).first)==string(str)) ans++;
}
out(ans);
mp.insert(pair<string, int>(str,i));
}
}*/
int main(){
in(n);
multiset <string> s;
rep(i,,n){
cin>>str;
sort(str.begin(),str.end());
ans=;
multiset <string>:: iterator it;
for(it=s.begin(); it != s.end(); it++) {
if(*it==str) ans++;
}
out(ans);
s.insert(str);
}
}

    后来在讨论区看到有一个叫做hash的东西,才开始百度现学hash,找了一个经典的hash函数,交了一发,A了,内心还是有点小激动的,毕竟第一次用,可能是数据比较弱再加上hash函数比较好吧,并没有发生传说中的冲突情况,在没接触过hash之前也想过对字符串进行编码,但发现不好编码,后来就放弃了这种思想,没想到最后还是通过hash函数编码来解决的

      

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define in(n) scanf("%d",&(n))
#define in2(x1,x2) scanf("%d%d",&(x1),&(x2))
#define inll(n) scanf("%I64d",&(n))
#define inll2(x1,x2) scanf("%I64d%I64d",&(x1),&(x2))
#define inlld(n) scanf("%lld",&(n))
#define inlld2(x1,x2) scanf("%lld%lld",&(x1),&(x2))
#define inf(n) scanf("%f",&(n))
#define inf2(x1,x2) scanf("%f%f",&(x1),&(x2))
#define inlf(n) scanf("%lf",&(n))
#define inlf2(x1,x2) scanf("%lf%lf",&(x1),&(x2))
#define inc(str) scanf("%c",&(str))
#define ins(str) scanf("%s",(str))
#define out(x) printf("%d\n",(x))
#define out2(x1,x2) printf("%d %d\n",(x1),(x2))
#define outf(x) printf("%f\n",(x))
#define outlf(x) printf("%lf\n",(x))
#define outlf2(x1,x2) printf("%lf %lf\n",(x1),(x2));
#define outll(x) printf("%I64d\n",(x))
#define outlld(x) printf("%lld\n",(x))
#define outc(str) printf("%c\n",(str))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define mem(X,Y) memset(X,Y,sizeof(X));
typedef vector<int> vec;
typedef long long ll;
typedef pair<int,int> P;
const int dx[]={,,-,},dy[]={,,,-};
const int INF=0x3f3f3f3f;
const ll mod=1e9+;
ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
const bool AC=true; int n,ans,len;
char s[];
unsigned int hash(char *str)
{
register unsigned int h;
register unsigned char *p; for(h=, p = (unsigned char *)str; *p ; p++)
h = * h + *p; return h;
}
int main(){
in(n);
map <int, int> mp;
rep(i,,n){
getchar();
scanf("%s",&s);
len=strlen(s);
sort(s,s+len);
out(mp[hash(s)]);
mp[hash(s)]++;
}
}

    下面来科普一下hash吧

    Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把任意长度的输入,通过散列算法,变换成固定长度的输出,该输出就是散列值。这种转换是一种压缩映射,也就是,散列值的空间通常远小于输入的空间,不同的输入可能会散列成相同的输出,所以不可能从散列值来唯一的确定输入值。简单的说就是一种将任意长度的消息压缩到某一固定长度的信息的函数。

    hash是一种采用空间换时间的思想,通过hash可以转化为O(1)的时间复杂度,常用于在较大的字符串集合中查找是否含有特定字符串。

    上面用的是一种类似times33的经典算法hash[i]=33*hash[i-1]+str[i](本题乘以的是31),除外还有Perl、Berkeley DB 、Apache、MFC、STL 等等。

字符串编码---hash函数的应用的更多相关文章

  1. JavaScript中有三个可以对字符串编码的函数,分别是: escape(),encodeURI(),encodeURIComponent()

    JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decod ...

  2. js 字符串编码转换函数

    escape 方法 对 String 对象编码以便它们能在所有计算机上可读, escape(charString) 必选项 charstring 参数是要编码的任意 String 对象或文字. 说明 ...

  3. JS 字符串编码函数(解决URL特殊字符传递问题):escape()、encodeURI()、encodeURIComponent()区别详解

    javaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decod ...

  4. JavaScript 字符串编码函数

    JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decod ...

  5. JavaScript中有对字符串编码的三个函数:escape,encodeURI,encodeURIComponent

    JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,decodeURI,decod ...

  6. 各种字符串Hash函数比较(转)

    常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法.这些函数使用位运算使得每一个字符都对最后的函数值产生影响.另外还有以MD5和SHA1为代表的杂凑函数,这些函数几乎 ...

  7. [转]各种字符串Hash函数比较

    转自:https://www.byvoid.com/zht/blog/string-hash-compare 常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法.这些 ...

  8. 【转】各种字符串Hash函数比较

    常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法.这些函数使用位运算使得每一个字符都对最后的函数值产生影响.另外还有以MD5和SHA1为代表的杂凑函数,这些函数几乎 ...

  9. [T]各种字符串Hash函数比较

    常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法.这些函数使用位运算使得每一个字符都对最后的函数值产生影响.另外还有以MD5和SHA1为代表的杂凑函数,这些函数几乎 ...

随机推荐

  1. web前端知识

    4.表格与表单 4.1 动态添加行 <script language=”javascript”> window.onload=function(){ var oTr = document. ...

  2. IDEA启动自动进入最后一个项目

    每次打开IDEA的时候总会加载上级最后打开的工程,可能这个工程并不是我需要的,我就得重新去打开我需要的工程,感觉这一点非常鸡肋. 使用如下方法可以在启动的时候,选择启动哪个工程,而不是直接进入. Fi ...

  3. 转:DSP学习经验

    转载:http://www.cnblogs.com/MrYang/archive/2010/12/21/1913035.html

  4. 解决:安装SQl 2008为SQL Server代理服务提供的凭据无效

    Q: 在Windows Server 2008安装SQL Server 2008出现的问题: 安装时在“服务器配置”环节出现以下问题:为sql server代理服务提供的凭据无效为sql server ...

  5. BNUOJ34990--Justice String (exkmp求最长公共前缀)

    Justice String Given two strings A and B, your task is to find a substring of A called justice strin ...

  6. html基础知识总结2

    下拉列表,文本域,复选框 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  7. RSA实例破解

    Description: Decode the message. You intercept the following message, which you know has been encode ...

  8. linux image writes boot log to console

  9. Tradesy | IT桔子

    Tradesy | IT桔子 Tradesy www.tradesy.com   认领 关注 分享

  10. split

    import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; /** * 解析知网文章的页面 ...