Cod sursa(job #100694)

Utilizator scvalexAlexandru Scvortov scvalex Data 12 noiembrie 2007 16:53:20
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Happy Coding 2007 Marime 1.57 kb
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <set>

using namespace std;

string text;
vector<unsigned long> cs;

unsigned long p[21];

multiset<unsigned long> twords;
set<unsigned long> words;

void calc_cksum(int n) {
	unsigned long c = 0;
	int i(0);
	unsigned long p = 1;
	for (; i < n; ++i) {
		c += (text[i] - 'a') * p;
		p *= 3;
	}
	p /= 3;
// 	cs.insert(cs.begin(), text.size() - n + 1, 0);
// 	cs[0] = c;
	twords.insert(c);
	for (; i < (int)text.size(); ++i) {
		c /= 3;
		c += p * (text[i] - 'a');
// 		cs[i - n + 1] = c;
		twords.insert(c);
	}
}

int main(int argc, char *argv[]) {
	ifstream fin("abc2.in");
	fin >> text;
	bool notCalculated(true);
	int tot(0);
	do {
		string aux;
		fin >> aux;
		if (!aux.empty()) {
			if (notCalculated) {
				calc_cksum(aux.size());
// 				for (int i(0); i < (int)cs.size(); ++i)
// 					cout << cs[i] << " ";
// 				cout << endl;
				notCalculated = false;

				p[0] = 1;
				for (int i(1); i <= (int)aux.size(); ++i)
					p[i] = p[i - 1] * 3;
			}

			unsigned long c = 0;
			for (int i(0); i < (int)aux.size(); ++i)
				c += (aux[i] - 'a') * p[i];

// 			cout << c << endl;
			words.insert(c);
		}
	} while (!fin.eof());
	fin.close();

	for (set<unsigned long>::const_iterator it = words.begin(); it != words.end(); ++it)
		tot += twords.count(*it);

// 	for (int i(0); i < (int)cs.size(); ++i)
// 		if (words.count(cs[i]))
// 			++tot;
// 	for (int i(0); i < (int)cs.size(); ++i)
// 		if (c == cs[i]) {
// 			cs[i] = -1;
// 			++tot;
// 		}

	ofstream fout("abc2.out");
	fout << tot << endl;
	fout.close();

	return 0;
}