-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontest2_task_y.cpp
More file actions
123 lines (120 loc) · 2.58 KB
/
contest2_task_y.cpp
File metadata and controls
123 lines (120 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<string.h>
namespace StringProcessing
{
std::vector<size_t> getZfunction(const std::string& str)
{
std::vector<size_t> z_function(str.length());
z_function[0] = z_function.size();
size_t left = 0;
size_t right = 0;
for (size_t i = 1; i < str.length(); i++)
{
size_t j = (size_t)std::max(std::min((int)(right - i), (int)z_function[i - left]), 0);
while (i + j < str.size() && str[j] == str[i + j])
{
j++;
}
if (i + j >= right)
{
left = i;
right = i + j;
}
z_function[i] = j;
}
return z_function;
}
template<class T>
std::vector<size_t> getZfunction(const std::vector<T>& str)
{
std::vector<size_t> z_function(str.size());
z_function[0] = z_function.size();
size_t left = 0;
size_t right = 0;
for (size_t i = 1; i < str.size(); i++)
{
size_t j = (size_t)std::max(std::min((int)(right - i), (int)z_function[i - left]), 0);
while (i + j < str.size() && str[j] == str[i + j])
{
j++;
}
if (i + j >= right)
{
left = i;
right = i + j;
}
z_function[i] = j;
}
return z_function;
}
}
template<class T>
void printVector(std::vector<T> vect)
{
for (T i : vect)
{
std::cout << i << " ";
}
std::cout << std::endl;
}
void isLastWord(const std::string& source_word, const std::string& jacks_word_)
{
std::string jacks_word = jacks_word_;
std::string summ_str = source_word + "#" + jacks_word;
std::vector<std::string> jacks_word_prefixes;
std::vector<size_t> z_function = StringProcessing::getZfunction(summ_str);
size_t prefixes_summ_size = 0;
size_t curr_size = z_function.size();
for (size_t i = z_function.size() - 1; i > source_word.size(); --i)
{
if (z_function[i] + i >= curr_size)
{
size_t curr_length = z_function[i] - curr_size + i;
std::string curr_prefix;
for (size_t j = i; j < curr_size; j++)
{
prefixes_summ_size++;
curr_prefix += summ_str[j];
}
jacks_word_prefixes.push_back(curr_prefix);
curr_size += curr_length - z_function[i];
}
}
if (prefixes_summ_size == jacks_word_.length())
{
std::cout << "No\n";
size_t i = jacks_word_prefixes.size() - 1;
while (1)
{
std::cout << jacks_word_prefixes[i] << " ";
if (i > 0)
{
i--;
}
else
{
break;
}
}
}
else
{
std::cout << "Yes\n";
}
}
int main()
{
std::ios_base::sync_with_stdio(0);
std::cin.tie(NULL);
std::cout.tie(NULL);
std::string source_word;
std::string jacks_word;
std::cin >> source_word;
std::cin >> jacks_word;
isLastWord(source_word, jacks_word);
return 0;
}