Pagini recente » Cod sursa (job #2148856) | Istoria paginii runda/lol | Istoria paginii runda/simulare-cartita-37 | Cod sursa (job #1559059) | Cod sursa (job #2433164)
#include <iostream>
#include <fstream>
#include <set>
using namespace std;
ofstream fout("abc2.out");
const int prime = 10001;
const int dictionarySize = 3;
int wordSize;
string sir;
set<string> hmap[10001];
int h = 1;
int hashf(string a)
{
int r=0;
for(int i = 0;i<a.size();i++)
r = (dictionarySize*r+a[i])%prime;
return r;
}
void calcH()
{
for(int i = 0;i<wordSize-1;i++)
h=(h*dictionarySize)%prime;
}
class InParser {
public:
FILE *fin;
char *buff;
int sp;
char read_ch() {
++sp;
if (sp == 4096) {
sp = 0;
fread(buff, 1, 4096, fin);
}
return buff[sp];
}
InParser(const char* nume) {
fin = fopen(nume, "r");
buff = new char[4096]();
sp = 4095;
}
};
void read()
{
InParser fin("abc2.in");
string temp;
char c;
c = fin.read_ch();
while(c!='\n')
sir+=c-'a'+1,c=fin.read_ch();
bool isDone = false;
while(c=fin.read_ch())
{
if(c=='\n')
{
if(!isDone){
wordSize = temp.size();
isDone=true;
}
hmap[hashf(temp)].insert(temp);
temp="";
}
else
{
temp+=c-'a'+1;
}
}
calcH();
}
int main()
{
read();
string temp = sir.substr(0,wordSize);
int f = hashf(temp);
int counter = 0;
for(int i = 0;i<=sir.size()-wordSize;i++)
{
temp = sir.substr(i,wordSize);
if(hmap[f].find(temp)!=hmap[f].end()){
counter++;
}
if(i<sir.size()-wordSize)
{
f =(dictionarySize*(f-sir[i]*h)+sir[i+wordSize])%prime;
while(f<0)
f+=prime;
}
}
fout<<counter<<'\n';
return 0;
}