Page 1 of 1

Palīdzība ar vienu algoritmu.

Posted: 05 Nov 2012, 21:45
by kristapuciitis
Sveiki! Man uzradās problēma atrisināt šo uzdevumu. Atrisināju šī uzdevuma vieglāko variantu, bet nevaru izdomāt algoritmu, lai atrisinātu pilno uzdevumu. Rekur kods vieglākajam variantam, risināju ar dinamisko programmēšanu, bet šķiet, ka pilnajam uzdevumam nederēs šāds līdzīgs piegājiens. Kādam ir kādas idejas?

Code: Select all

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
	freopen("lielkva.dat", "r", stdin);
	freopen("lielkva.rez", "w", stdout);

	string lastRow;
	string currentRow;
	vector<int> lastCount;
	vector<int> currentCount;
	int n, max = 1;

	cin >> n;
	cin >> lastRow;
	for (int i = 0; i < n; i++)
	{
		lastCount.push_back(1);
		currentCount.push_back(1);
	}

	for (int i = 1; i < n; i++)
	{
		cin >> currentRow;
		currentCount[0] = 1;

		for (int j = 1; j < n; j++)
		{
			if (currentRow[j] == currentRow[j-1] &&
				currentRow[j] == lastRow[j] &&
				currentRow[j] == lastRow[j-1])
			{
				currentCount[j] = min(currentCount[j-1], min(lastCount[j], lastCount[j-1])) + 1;
			}
			else
			{
				currentCount[j] = 1;
			}

			if (currentCount[j] > max)
			{
				max = currentCount[j];
			}
		}

		lastCount = currentCount;
		lastRow = currentRow;
	}
	
	cout << max;

	return 0;
};