Cod sursa(job #343442)

Utilizator prdianaProdan Diana prdiana Data 25 august 2009 21:45:01
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include <fstream>
#include <string>
#include <vector>
#define MOD 6001

using namespace std;

ifstream fin("abc2.in");
ofstream fout("abc2.out");

vector<string> h[MOD];
int len,i;
string text,cuvant;

void hash(const string &key)
{
	int val = 0;
	for (i=0;i<len;i++)
	{
		val+=key[i];
	}
	h[val].push_back(key);
}


bool find(int key, const string &obj)
{

	if (h[key].size()>0)
	{
		int k;
		for (k=0;k<h[key].size();k++)
		{
			if (h[key][k] == obj)
			{
				return true;
			}
		}
	}
	return false;
}

int nrsol;

int main()
{
	
	fin >> text;
	
	while (!fin.eof())
	{
		fin >> cuvant;
		len = cuvant.size();
		hash(cuvant);
	}
	int key = 0,end;
	string obj;
	for (i=0;i<len;i++)
	{
		obj.push_back('a');
	}
	end = text.size() - len;

	int j,t;
	for (i=0;i<=end;i++)
	{
		t = 0;
		key = 0;
		for (j=i;j<i+len;j++)
		{
			obj[t] = text[j]; 
			key+=obj[t];
			t++;
		}
		if (find(key,obj))
		{
			nrsol++;
		}
	}
	fout << nrsol;
	return 0;
}