Pagini recente » Cod sursa (job #527239) | Cod sursa (job #1653885)
#include <iostream>
#include <vector>
#include <algorithm>
#include <limits>
#include <numeric>
#include <cstring>
#include <string>
#include <queue>
#include <set>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <map>
#define pb push_back
#define mp make_pair
#define INF numeric_limits<int>::max()
#define bit(x) (-x)&x
#define int64 long long
using namespace std;
ifstream in("abc2.in");
ofstream out("abc2.out");
string s,x;
int n,m;
#define mod1 666013
#define mod2 587697
#define P 101
vector< vector< pair<int,int> > > h(mod1);
vector< vector<int> > t(mod1);
bool ok(int c1,int c2)
{
for(int i=0;i<(int)t[c1].size();i++)
if(t[c1][i]==c2)
return false;
t[c1].pb(c2);
return true;
}
void ins(int c1,int c2)
{
for(int i=0;i<(int)h[c1].size();i++)
if(h[c1][i].first==c2)
{
h[c1][i].second++;
return;
}
h[c1].pb(mp(c2,1));
}
int cnt(int c1,int c2)
{
for(int i=0;i<(int)h[c1].size();i++)
if(h[c1][i].first==c2)
return h[c1][i].second;
return 0;
}
void addwords(int m)
{
int p1=1,p2=1,c1=0,c2=0;
for(int i=m-1;i>=0;i--)
{
c1=(c1+s[i]*p1)%mod1;
c2=(c2+s[i]*p2)%mod2;
if(i!=0)
{
p1=(p1*P)%mod1;
p2=(p2*P)%mod2;
}
}
ins(c1,c2);
for(int i=m;i<(int)s.size();i++)
{
c1=((c1-(s[i-m]*p1)%mod1+mod1)*P+s[i])%mod1;
c2=((c2-(s[i-m]*p2)%mod2+mod2)*P+s[i])%mod2;
ins(c1,c2);
}
}
int main()
{
in>>s;
for(int i=1;i<=20;i++)
addwords(i);
int sol=0;
while(in>>x)
{
int p1=1,p2=1,c1=0,c2=0;
for(int i=x.size()-1;i>=0;i--)
{
c1=(c1+p1*x[i])%mod1;
c2=(c2+p2*x[i])%mod2;
p1=(P*p1)%mod1;
p2=(P*p2)%mod2;
}
if(ok(c1,c2))
sol+=cnt(c1,c2);
}
out<<sol<<'\n';
return 0;
}