CSES - Datatähti 2019 alku - Results
Submission details
Task:Leimasin
Sender:Ilmari2000
Submission time:2018-10-08 23:28:47 +0300
Language:C++
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:15:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i <= target.size() - stamp.size(); i++)
                 ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
input/code.cpp:21:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = target.size() - 1; i >= stamp.size(); i--)
                                 ~~^~~~~~~~~~~~~~~
input/code.cpp:27:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i = 0; i <= target.size() - stamp.size(); i++)
                 ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
input/code.cpp:30:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int j = 0; j < stamp.size(); j++)
                  ~~^~~~~~~~~~~~~~
input/code.cpp:52:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < s...

Code

#include <deque>
#include <iostream>
#include <map>
#include <string>

using namespace std;

int main()
{
	string target, stamp;
	cin >> target >> stamp;

	deque<int> order1, order2, order3, order4, order5;

	for(int i = 0; i <= target.size() - stamp.size(); i++)
	{
		if(stamp[0] == target[i])
			order2.push_back(i);
	}

	for(int i = target.size() - 1; i >= stamp.size(); i--)
	{
		if(stamp[stamp.size() - 1] == target[i])
			order1.push_back(i - stamp.size() + 1);
	}

	for(int i = 0; i <= target.size() - stamp.size(); i++)
	{
		bool flag = true;
		for(int j = 0; j < stamp.size(); j++)
		{
			if(target[i+j] != stamp[j])
			{
				flag = false;
				break;
			}
		}

		if(flag)
			order3.push_back(i);
	}

	deque<int> order;
	for(int p : order1) order.push_back(p);
	for(int p : order2) order.push_back(p);
	for(int p : order3) order.push_back(p);


	string comp(target.size(), ' ');
	for(int p : order)
	{
		for(int i = 0; i < stamp.size(); i++)
			comp[p+i] = stamp[i];
	}
	//cout << comp << endl;

	for(int i = 0; i <= target.size() - stamp.size(); i++)
	{
		int p = 0;
		string w;
		for(; comp[i+p] != target[i+p] && p < stamp.size(); p++)
			w += target[i+p];

		if(p)
		{
			int q = 0;
		a:
			for(int j = 0; j + q < stamp.size() && j < w.size(); j++)
			{
				if(stamp[j+q] != w[j])
				{
					q++;
					goto a;
				}
			}

			bool flag = true;
			for(int j = 0; j < p && i + j < n; j++)
			{
				if(comp[i + j] != ' ')
				{
					flag = false;
					break;
				}
			}

			(flag ? order5 : order4).push_back(i - q);
		}

		i += p;
		
	}

	order.clear();
	for(int p : order5) order.push_back(p);
	for(int p : order1) order.push_back(p);
	for(int p : order4) order.push_back(p);
	for(int p : order2) order.push_back(p);
	for(int p : order3) order.push_back(p);

	for(int p : order)
	{
		for(int i = 0; i < stamp.size(); i++)
			comp[p+i] = stamp[i];
	}

	//cout << comp << endl;
	if(comp != target)
	{
		cout << "-1" << endl;
		return 0;
	}
	
	cout << order.size() << endl;	
	for(int p : order)
		cout << p+1 << " ";
	cout << endl;

	return 0;
}