-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontest2_task_X.cpp
More file actions
123 lines (115 loc) · 2.62 KB
/
contest2_task_X.cpp
File metadata and controls
123 lines (115 loc) · 2.62 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;
}
std::string getReversedStr(const std::string& str)
{
std::string reversed_string = str;
std::reverse(reversed_string.begin(), reversed_string.end());
return reversed_string;
}
template<class T>
std::vector<T> getReversedStr(const std::vector<T>& str)
{
std::vector<T> reversed_str = str;
std::reverse(reversed_str.begin(), reversed_str.end());
return reversed_str;
}
}
template<class T>
std::vector<size_t> getCubesCount(const std::vector<T>& str)
{
std::vector<T> curr_str;
for (size_t i = 0; i < str.size(); i++)
{
curr_str.push_back(str[i]);
}
curr_str.push_back(-1);
for (size_t i = 0; i < str.size(); i++)
{
curr_str.push_back(str[str.size() - i - 1]);
}
std::vector<size_t> z_function = StringProcessing::getZfunction(curr_str);
std::vector<size_t> cubes_sizes = { str.size() };
for (size_t i = str.size(); i < curr_str.size(); i++)
{
if (z_function[i] % 2 == 0 && z_function[i] + i == curr_str.size())
{
cubes_sizes.push_back(str.size() - z_function[i] / 2);
}
}
std::sort(cubes_sizes.begin(), cubes_sizes.end());
return cubes_sizes;
}
template<class T>
void printVector(std::vector<T> vect)
{
for (T i : vect)
{
std::cout << i << " ";
}
std::cout << std::endl;
}
int main()
{
std::ios_base::sync_with_stdio(0);
std::cin.tie(NULL);
std::cout.tie(NULL);
size_t n, m;
std::cin >> n >> m;
std::vector<size_t> str(n);
for (size_t i = 0; i < n; i++)
{
std::cin >> str[i];
}
printVector(getCubesCount(str));
}