Cod sursa(job #100685)

Utilizator scvalexAlexandru Scvortov scvalex Data 12 noiembrie 2007 16:45:31
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Happy Coding 2007 Marime 1.49 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> 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;
	words.insert(c);
// 	cs.insert(cs.begin(), text.size() - n + 1, 0);
// 	cs[0] = c;
	for (; i < (int)text.size(); ++i) {
		c /= 3;
		c += p * (text[i] - 'a');
		words.insert(c);
// 		cs[i - n + 1] = 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);

			tot += words.count(c);
			words.erase(c);

// 			for (int i(0); i < (int)cs.size(); ++i)
// 				if (c == cs[i]) {
// 					cs[i] = -1;
// 					++tot;
// 				}
		}
	} while (!fin.eof());
	fin.close();

// 	for (int i(0); i < (int)cs.size(); ++i)
// 		if (words.count(cs[i]))
// 			++tot;

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

	return 0;
}