Submission #805337

# Submission time Handle Problem Language Result Execution time Memory
805337 2023-08-03T15:27:02 Z Tunglam07 Bajka (COCI20_bajka) C++17
20 / 70
1000 ms 300 KB
#include<bits/stdc++.h>
using namespace std;
 
long long n, m, minn = INT_MAX;
string s, t;
vector<int> ch[27];
 
long long backtrack(int id, int pos)
{
	if(pos == m)	
	{
		return 0;
	}
	long long ans = INT_MAX;
	for(int i : ch[s[id] - 96])
	{
		if(i < n - 1 && s[i + 1] == t[pos])	
		{
			ans = min(ans, backtrack(i + 1, pos + 1) + abs(id - i) + 1);
		}
		if(i > 0 && s[i - 1] == t[pos])	
		{
			ans = min(ans, backtrack(i - 1, pos + 1) + abs(id - i) + 1);
		}
	}
	return ans;
}
 
int main()
{
	cin >> n >> m;
	cin>> s; 
	cin>> t;
	for(int i = 0; i < n; i++)	
	{
		ch[s[i] - 96].push_back(i);
	}
	for(int c : ch[t[0] - 96])	
	{
		minn = min(minn, backtrack(c, 1));
	}
	if(minn < INT_MAX)	
	{
		cout << minn;
		return 0;
	}
	cout << -1;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 300 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1066 ms 212 KB Time limit exceeded
2 Halted 0 ms 0 KB -