昨天说今天要把hash和KMP学会,那么学会了吗?很明显没有,hash已经有点头绪了,但是KMP一直都搞不明白,就算看图解也看不明白,跳的有点厉害把我跳的有点晕乎乎的。不过好歹也勉强把“模板”拉过来了,真正理解应该还要一段时间。
这道KMP,让我十分的疑惑,搞了很久很久也一直搞不明白。看了图解也看不明白的那种。只好去把书上的模板勉强拉过来用了,然后发现答案错了又错(样例错误),改了好久好久才把代码写好,样例才过的去,不过还好,测试点都过了。说实话,我现在还是对KMP不理解,之后得好好看看,看一下能不能理解了。
#include <bits/stdc++.h>
using namespace std;
const long long range = 1000010;
string S, T;
int nextval[range] = {0};
void GetNext(string T)
{
nextval[0] = -1;
int j = -1;
for(int i = 1;i < T.size();i ++)
{
while(j != -1 && T[i] != T[j + 1])
{
j = nextval[j];
}
if(T[i] == T[j + 1]) j ++;
nextval[i] = j;
}
}
void KMP(string S, string T)
{
int lenS = S.size(), lenT = T.size();
int j = -1;
GetNext(T);
for(int i = 0;i < lenS;i ++)
{
while(j != -1 && S[i] != T[j + 1])
{
j = nextval[j];
}
if(S[i] == T[j + 1])
{
j ++;
}
if(j == lenT - 1)
{
cout << i - lenT + 2 << endl;
j = nextval[j];
}
}
}
int main()
{
cin >> S >> T;
KMP(S, T);
for(int i = 0;i < T.size();i ++)
{
cout << nextval[i] + 1 << " ";
}
return 0;
}
好的来看下一题。
这道题就相对简单很多了嘛。
原本想着可以用哈希做的,但是感觉哈希太麻烦了,看了一下数据范围,发现可以取捷径走。哈希是对应映射的,这就让我想到了C++里面一个很好用的东西:map。封装好的为什么不用呢。因为map也是一一映射的,所以也可以做到类似的效果。只要让map映射string和struct就行了,每个人就有自己的两个数据:名字、被点名次数。只要在前面输入的时候进行处理和映射就行,后面检查的时候直接检查map里有没有这个成员,有的话,再检查一下他有没有被点名就可以。上代码。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct students {
string name;
int times;
} stu[100001];
int main()
{
map<string, struct students> mp;
ll n, m;
cin >> n;
getchar();
for(ll i = 0;i < n;i ++)
{
string temp;
cin >> temp;
stu[i].name = temp;
stu[i].times = 0;
mp[temp] = stu[i];
}
cin >> m;
getchar();
for(ll i = 0;i < m;i ++)
{
string temp;
cin >> temp;
map<string, struct students>::iterator iter = mp.find(temp); //这里检查有没有这个成员
if(iter != mp.end()) //---------------------
{
if(mp[temp].times == 1)
{
cout << "REPEAT" << endl;
}
else
{
cout << "OK" << endl;
mp[temp].times ++;
}
}
else
{
cout << "WRONG" << endl;
}
}
return 0;
}
这道题的数据范围其实也可以用map做的,但是因为它是一道hash题,那就用hash做吧。其实这道题目花费了比我想象中要久的多的时间。
我刚开始的代码是这样子的:
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1313131;
bool a[10000000] = {0}; //记录是否出现
//以0为底
int main()
{
int n;
cin >> n;
getchar();
int sum = 0;
for(int i = 0;i < n;i ++)
{
string temp;
cin >> temp;
long long hash = 0;
for(int j = 0;j < temp.size();j ++)
{
hash = (hash * 131 + (temp[j] - '0')) % mod;
}
if(!a[hash])
{
a[hash] = true;
sum ++;
}
}
cout << sum;
return 0;
}
自信满满地交上去了,然后发现:
"那一定是hash值重复了!"我这么想着,所以我想着去换一个hash算法。
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1313131;
bool a[10000000] = {0}; //记录是否出现
//以0为底
int main()
{
int n;
cin >> n;
getchar();
int sum = 0;
for(int i = 0;i < n;i ++)
{
string temp;
cin >> temp;
long long hash = 0;
for(int j = 0;j < temp.size();j ++)
{
hash = (hash * 16 + (temp[j] - '0' + 1)) % mod;
}
if(!a[hash])
{
a[hash] = true;
sum ++;
}
}
cout << sum;
return 0;
}
我这次换成了16进制的了,但是发现还是WA。
然后我又搞了好久好久... ...
最后找了个比较好的hash算法。
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const long long mod = 16777619;
bool a[20000000] = {0}; //记录是否出现
//以0为底
int main()
{
int n;
cin >> n;
getchar();
int sum = 0;
for(int i = 0;i < n;i ++)
{
string temp;
cin >> temp;
long long hash = 0;
for(int j = 0;j < temp.size();j ++)
{
hash = (hash * 163 + (temp[j] - '0')) % mod;
}
if(!a[hash])
{
a[hash] = true;
sum ++;
}
}
cout << sum;
return 0;
}
把base值换成了163,然后改大了范围。
虽然没过,但是好一些了。
然后又试了很久很久很久... ...它就是不过,不论我怎么换base值和mod值。于是我就去查了其他的hash算法和推荐,然后发现我之前的写法一直有问题,打算试一下用单哈希的模板。运用unsigned long long的自然溢出,可以做到不需要取模也可以最大范围的减少哈希值碰撞的可能。
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const ull base = 163;
const long long mod = 212370440130137957ll;
ull hashlist[10001];
int main()
{
int n;
cin >> n;
getchar();
int sum = 1;
for(int i = 0;i < n;i ++)
{
string temp;
cin >> temp;
ull hash = 0;
for(int j = 0;j < temp.size();j ++)
{
hash = hash * base + (ull)temp[j];
}
hashlist[i] = hash;
}
sort(hashlist, hashlist + n);
for(int i = 1;i < n;i ++)
{
if(hashlist[i] != hashlist[i - 1]) sum ++;
}
cout << sum;
return 0;
}
然后就AC了,不得不说很爽,因为卡了挺久的。
然后又去尝试做了一下A-B的那道题,发现不能用朴素算法过又没有什么好的想法,感觉跟hash相关但又感觉没有,明天再看看有没有思路和灵感吧,今天累了。
明天把题目做完!