Cod sursa(job #1455572)

Utilizator aimrdlAndrei mrdl aimrdl Data 28 iunie 2015 14:58:24
Problema Potrivirea sirurilor Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.12 kb
#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;

#define MAX 2000000

unsigned int strMatch (const char *s1, const char *s2, vector <unsigned int> & v) {
	unsigned int l1 = strlen(s1), l2 = strlen(s2);
	
	unsigned int matchNo = 0;
	
	for (unsigned int i = 0; i < l2; ++i) {
		if (s2[i] == s1[0]) {
			unsigned int match = 1, j = 1;
			while (match && j < l1) {
				if (s2[i + j] != s1[j]) match = 0;
				++j;
			}
			
			if (match) {
				++matchNo;
				v.push_back(i);
			}
		}
	}
	
	return matchNo;
}
 
int main (void) {
	FILE *in = fopen("strmatch.in", "r");
	char *buffer = new char[MAX];
	
	fgets(buffer, MAX, in);
	buffer[strlen(buffer) - 1] = '\0';
	char *s1 = strdup(buffer);
	
	fgets(buffer, MAX, in);
	buffer[strlen(buffer) - 1] = '\0';
	char *s2 = strdup(buffer);
	
	delete[] buffer;
	fclose(in);
	
	FILE *out = fopen("strmatch.out", "w");
	
	vector <unsigned int> v;
	unsigned int matchNo = strMatch(s1, s2, v);
	
	fprintf(out, "%d\n", matchNo);
	for (unsigned int i = 0; i < matchNo && i < 1000; ++i) {
		fprintf(out, "%d ", v[i]);
	}
	
	delete[] s1;
	delete[] s2;
	v.clear();	
	fclose(out);
	return 0;
}