text stringlengths 49 983k |
|---|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int k, count = 0;
char ch;
cin >> k;
string s;
cin >> s;
int l = s.size();
sort(s.begin(), s.begin() + s.size());
for (int i = 0; i < s.size(); i++) {
if (i % k == 0) {
ch = s[i];
}
if (s[i] == ch) {
count++;
}
}
if (l == count && count % k == 0) {
for (int i = 0; i < k; i++) {
for (int l = 0; l < s.size(); l += k) {
cout << s[l];
}
}
} else {
cout << "-1";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int k, flg = 1;
cin >> k;
string s;
cin >> s;
int n = s.size();
map<char, int> m;
set<char> a;
for (int i = 0; i < n; ++i) {
++m[s[i]];
a.insert(s[i]);
}
for (auto &x : a)
if (m[x] % k != 0) {
flg = 0;
break;
}
if (flg == 1)
for (int i = 0; i < k; ++i)
for (auto &x : a) {
int l = m[x] / k;
while (l--) cout << x;
}
else
cout << -1;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int k;
string s, ans = "";
cin >> k >> s;
int freq[30] = {};
for (int i = 0; i < s.size(); i++) freq[s[i] - 'a']++;
for (char c = 'a'; c <= 'z'; c++) {
if (freq[c - 'a'] % k != 0) return cout << -1, 0;
ans += string(freq[c - 'a'] / k, c);
}
for (int i = 0; i < k; i++) cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int k;
cin >> k;
string s;
string ll;
cin >> s;
int l = s.size();
int a[27] = {0};
for (int i = 0; i < l; i++) {
a[s[i] - 96] += 1;
}
int i = 0;
for (i = 1; i < 27; i++) {
if (a[i] % k != 0) break;
}
if (i < 27) {
cout << -1 << endl;
} else {
for (int i = 1; i < 27; i++) ll.append(a[i] / k, i + 96);
}
for (int i = 0; i < k; i++) cout << ll;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
const long long mod = 1e9 + 7;
long long fpow(long long a, long long n) {
if (n == 0) return 1;
if (n % 2 == 1)
return (a * fpow(a * a % mod, n / 2)) % mod;
else
return fpow(a * a % mod, n / 2);
}
long long T = 1;
void unstoppable() {
long long n;
string s;
cin >> n >> s;
if (n == 1) {
cout << s;
return;
}
map<char, long long> mp;
for (char c : s) mp[c]++;
string ans = "";
for (auto e : mp)
if (e.second % n != 0) {
cout << -1;
return;
}
map<char, long long> vis;
for (long long i = 0; i < s.size(); ++i) {
if (!vis[s[i]]) {
vis[s[i]] = 1;
long long tmp = mp[s[i]] / n;
for (long long j = 0; j < tmp; ++j) ans += s[i];
}
}
while (n--) {
cout << ans;
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
while (T--) {
unstoppable();
}
}
|
#include <bits/stdc++.h>
using namespace std;
int num(char c, char a[], int k) {
int count = 0;
for (int i = 0; i < k; i++) {
if (a[i] == c) count++;
}
return count;
}
int main() {
int n;
bool check = true;
cin >> n;
string s, ans;
cin >> s;
int k = s.length();
char c[k];
for (int i = 0; i < k; i++) c[i] = s[i];
set<char> a(begin(s), end(s));
for (set<char>::iterator itr = a.begin(); itr != a.end(); ++itr) {
if (num(*itr, c, k) % n != 0) {
check = false;
break;
}
int p = num(*itr, c, k) / n;
string g(1, *itr);
for (int l = 0; l < p; l++) ans.append(g);
}
string f;
if (check) {
for (int i = 0; i < n; i++) f.append(ans);
cout << f;
} else
cout << -1;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
int k;
string n;
cin >> k;
cin >> n;
int len = n.length(), tmp = 1;
char arr[10000], diffArr[1000];
for (int i = 0; i < len; ++i) {
arr[i] = n[i];
}
sort(arr, arr + len);
int countChar[1000], countT = 0;
for (int i = 0; i < len - 1; ++i) {
if (arr[i] == arr[i + 1])
tmp++;
else {
diffArr[countT] = arr[i];
countChar[countT] = tmp;
countT++;
tmp = 1;
}
}
tmp = 1;
for (int i = len - 1; i >= 0; --i) {
if (arr[i] == arr[i - 1])
tmp++;
else {
diffArr[countT] = arr[i];
countChar[countT] = tmp;
countT++;
break;
}
}
bool check = true;
for (int i = 0; i < countT; ++i) {
if (countChar[i] % k != 0) {
check = false;
break;
}
}
if (!check) {
cout << -1;
return 0;
}
for (int i = 0; i < k; ++i) {
for (int t = 0; t < countT; ++t) {
for (int j = 0; j < countChar[t] / k; ++j) {
cout << diffArr[t];
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int k;
string s;
cin >> k;
cin >> s;
vector<int> dic(40);
int l = s.size();
for (int i = 0; i < l; i++) {
dic[s[i] - 'a']++;
}
string outStr = "";
bool flag = true;
for (int i = 0; i < 40; i++) {
if (dic[i] % k != 0) {
flag = false;
break;
}
for (int j = 0; j < dic[i] / k; j++) outStr += i + 'a';
}
if (flag)
for (int i = 0; i < k; i++) cout << outStr;
else
cout << "-1";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int c[26] = {0};
int main() {
int k;
cin >> k;
string s;
cin >> s;
int l = s.length();
for (int i = 0; i < l; i++) {
int x = (int)(s[i] - 'a');
c[x]++;
}
for (int i = 0; i < 26; i++) {
if (c[i] % k != 0) {
cout << -1;
return 0;
}
}
string an = "";
for (int i = 0; i < 26; i++) {
int a = c[i] / k;
int le = an.length();
char ch = (char)(i + 'a');
an.insert(le, a, ch);
}
string ans = "";
for (int i = 1; i <= k; i++) {
ans += an;
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int k;
cin >> k;
string s, s1 = "";
cin >> s;
std::vector<int> v(26);
for (int i = 0; s[i]; i++) v[s[i] - 'a']++;
for (int i = 0; i < 26; i++) {
if (v[i] % k) {
cout << "-1" << endl;
return 0;
}
}
for (int i = 0; i < 26; i++) {
for (int j = 0; j < v[i] / k; j++) s1 += i + 'a';
}
for (int i = 0; i < k; i++) cout << s1;
cout << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int alphabet[26] = {0};
int key = 0, flag = 1;
cin >> key;
string s;
cin >> s;
for (int i = 0; s[i] != 0; i++) {
alphabet[s[i] - 97]++;
}
for (int i = 0; i < 26; i++) {
if (alphabet[i] % key != 0) {
cout << -1 << endl;
flag = 0;
break;
}
}
if (flag) {
for (int i = 0; i < key; i++) {
for (int j = 0; j < 26; j++) {
if (alphabet[j] > 0) {
for (int k = 0; k < alphabet[j] / key; k++) cout << char(97 + j);
}
}
}
}
cout << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void set1(int alpha[]) {
for (int i = 0; i < 26; i++) alpha[i] = 0;
}
void set2(int alpha[], char s[]) {
for (int i = 0; s[i]; i++) {
alpha[s[i] - 97]++;
}
}
bool check(int alpha[], int k) {
for (int i = 0; i < 26; i++) {
if (alpha[i] % k != 0) return false;
alpha[i] /= k;
}
return true;
}
void display(int alpha[]) {
for (int i = 0; i < 26; i++) {
for (int j = 0; j < alpha[i]; j++) {
cout << char(i + 97);
}
}
}
int main() {
int k;
char s[1100];
int alpha[26];
set1(alpha);
cin >> k;
cin >> s;
set2(alpha, s);
if (check(alpha, k)) {
for (int i = 0; i < k; i++) display(alpha);
} else {
cout << -1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int k, len, t, cp[1009];
bool ok, vis[1009];
char str[1009];
cin >> k;
cin >> str;
len = strlen(str);
memset(cp, 0, sizeof(cp));
memset(vis, 0, sizeof(vis));
for (int i = 0; i < len; i++) {
int t = str[i] - 'a';
vis[t] = 1;
cp[t]++;
}
ok = true;
for (int i = 0; i < 26; i++) {
if (vis[i] & cp[i] % k != 0) {
ok = false;
break;
}
}
if (ok) {
sort(str, str + len);
for (int i = 0; i < k; i++)
for (int j = 0; j < len; j += k) {
cout << str[j];
}
} else
printf("-1\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long n;
cin >> n;
string s;
cin >> s;
if (n == 1)
cout << s << "\n";
else {
if (s.length() % n)
cout << "-1"
<< "\n";
else {
vector<long long> v(26, 0);
for (long long i = 0; i < s.length(); i++) v[s[i] - 'a']++;
for (long long i = 0; i < v.size(); i++) {
if (v[i] % n) {
cout << "-1"
<< "\n";
return 0;
}
}
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < v.size(); j++) {
char ch = j + 'a';
for (long long k = 0; k < v[j] / n; k++) {
cout << ch;
}
}
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
string s;
int k;
int f[500];
int main() {
cin >> k;
cin >> s;
int len = s.size();
for (int i = 0; i < len; i++) f[s[i] - 'a']++;
for (int i = 0; i < 26; i++)
if (f[i] % k != 0) {
cout << -1;
return 0;
}
for (int k1 = 1; k1 <= k; k1++)
for (int i = 0; i < 26; i++)
for (int j = 1; j <= (f[i] / k); j++) cout << (char)('a' + i);
}
|
#include <bits/stdc++.h>
using namespace std;
void OpenFiles() {}
char x[1 << 10], sol[1 << 10];
int f[30];
int main() {
OpenFiles();
int K, lenth, i, j, ind;
char ch;
scanf("%d\n", &K);
gets(x + 1);
int N = strlen(x + 1);
if (N % K) {
printf("-1");
return 0;
}
lenth = N / K;
bool isSolution = 1;
for (i = 1; i <= N; i++) f[x[i] - 'a']++;
for (i = 1; i <= lenth; i++) {
for (j = 0; j < 30; j++)
if (f[j]) {
ch = (char)j + 'a';
ind = j;
}
for (j = i; j <= N; j += lenth)
if (f[ind] == 0) {
isSolution = 0;
break;
} else {
sol[j] = ch;
f[ind]--;
}
if (isSolution == 0) {
printf("-1");
return 0;
}
}
for (i = 1; i <= N; i++) printf("%c", sol[i]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, ltr[30] = {0}, u;
string s;
cin >> n;
cin >> s;
for (int i = 0; i < s.length(); i++) {
int p = s[i] - 'a';
ltr[p]++;
}
for (int i = 0; i < 26; i++) {
if (ltr[i] % n != 0) {
cout << -1 << endl;
return 0;
}
}
for (int k = 0; k < n; k++) {
for (int j = 0; j < 26; j++) {
if (ltr[j] > 0) {
for (int i = 0; i < ltr[j] / n; i++) {
char w = j + 97;
cout << w;
}
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(NULL), cin.tie(NULL), cout.tie(NULL);
int n;
cin >> n;
string c;
cin >> c;
vector<int> isp(27, 0);
for (int i = 0; i < c.length(); ++i) {
isp[c[i] - 'a']++;
}
for (int i = 0; i < 26; ++i) {
if (isp[i] % n != 0) {
cout << "-1\n";
return 0;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < 26; j++) {
if (isp[j] != 0) {
for (int r = 0; r < (isp[j] / n); r++) {
cout << (char)(j + 'a');
}
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int k;
cin >> k;
string s;
cin >> s;
int i;
int len = s.length();
if (len % k != 0) {
cout << -1 << "\n";
return 0;
}
int new_len = len / k;
vector<int> c(26, 0);
for (i = 0; i < len; i++) {
c[s[i] - 'a'] += 1;
}
string ans = "";
for (i = 0; i < 26; i++) {
if (c[i] != 0) {
if (c[i] % k == 0) {
int j = c[i] / k;
while (j > 0) {
ans += (i + 'a');
j--;
}
} else {
cout << -1 << "\n";
return 0;
}
}
}
string temp = ans;
for (i = 1; i < k; i++) {
ans += temp;
}
cout << ans << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int k;
string s;
vector<int> count(26);
cin >> k >> s;
for (char i : s) count[i - 97]++;
s = "";
for (int i = 0; i < 26; ++i) {
if (count[i] % k > 0) {
cout << -1 << '\n';
return 0;
}
for (int j = 1; j <= count[i] / k; ++j) {
s += i + 97;
}
}
for (int i = 1; i <= k; ++i) cout << s;
cout << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int k;
int c[26] = {0};
int main() {
cin >> k;
char ch;
while (cin >> ch) {
c[ch - 'a']++;
}
for (int i = 0; i <= 25; i++) {
if (c[i] % k != 0) {
cout << -1 << endl;
return 0;
}
}
for (int i = 1; i <= k; i++) {
for (int j = 0; j <= 25; j++) {
for (int l = 1; l <= c[j] / k; l++) {
cout << char('a' + j);
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void input() {}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
input();
int n, k;
cin >> k;
string str, ans = "";
cin >> str;
n = str.length();
map<char, int> m;
if (n == 1)
cout << str;
else if (n % k != 0)
cout << -1;
else {
int flag = 1;
for (long long int i = 0; i < n; i++) m[str[i]]++;
for (auto j : m) {
if (j.second % k != 0) {
flag = 0;
break;
} else {
string temp(j.second / k, j.first);
ans += temp;
}
}
if (flag) {
for (long long int i = 0; i < n / ans.length(); i++) cout << ans;
} else
cout << -1;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2000;
char a[maxn], b[maxn];
int c[maxn];
int main() {
int n, cnt1 = 1, flag1 = 0;
cin >> n >> a;
memset(c, 0, sizeof(c));
sort(a, a + strlen(a));
for (int i = 0; i < strlen(a); i++) {
if (a[i] == a[i + 1]) {
cnt1++;
} else {
if (cnt1 % n == 0) {
c[i] = cnt1;
cnt1 = 1;
} else {
cout << "-1" << endl;
flag1 = 1;
break;
}
}
}
int p = 0;
if (!flag1) {
for (int j = 0; j < strlen(a); j++) {
if (c[j] != 0) {
for (int k = 1; k <= c[j] / n; k++) {
b[p++] = a[j];
}
}
}
for (int m = 0; m < n; m++) {
cout << b;
}
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int k;
cin >> k;
string str;
cin >> str;
map<char, int> mp;
for (int i = 0; i < str.size(); ++i) mp[str[i]]++;
bool exists = str.size() % k == 0;
string sol = "";
for (auto letter : mp) {
if (letter.second % k == 0) {
sol += string(letter.second / k, letter.first);
} else {
exists = false;
break;
}
}
if (exists) {
for (int i = 0; i < k; ++i) cout << sol;
} else
cout << -1;
cout << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string input;
int k, letters[26] = {0};
cin >> k >> input;
for (int i = 0; i < input.size(); i++) letters[input[i] - 'a']++;
for (int i = 0; i < 26; i++)
if (letters[i] % k != 0) {
cout << "-1";
return 0;
}
for (int i = 0; i < k; i++)
for (int j = 0; j < 26; j++)
for (int m = 0; m < letters[j] / k; m++) cout << (char)(j + 'a');
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string x;
cin >> x;
string arr[n];
sort(x.begin(), x.end());
for (int i = 0; i < x.length();) {
for (int b = 0; b < n; b++, i++) {
if (i < n)
arr[b] = x[i];
else
arr[b] += x[i];
}
}
int c = 0;
string res = "";
for (int b = 0; b < n - 1; b++) {
res += arr[b];
if (arr[b] != arr[b + 1]) {
c = 1;
}
}
res += arr[n - 1];
if (c == 0)
cout << res;
else
cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
string s, ans;
long long n, q[10000];
map<char, int> cnt;
int main() {
cin >> n;
cin >> s;
if (n == 1) {
cout << s;
return 0;
}
for (int i = 0; i < s.length(); i++) {
q[int(s[i]) - 97]++;
}
for (int i = 0; i < s.length(); i++) {
if (q[i] % n != 0) {
cout << -1;
return 0;
}
}
for (int i = 0; i < 26; i++) {
for (int j = 0; j < q[i] / n; j++) {
ans += char(i + 97);
}
}
for (int i = 1; i <= n; i++) {
cout << ans;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
int dblcmp(double d) {
if (fabs(d) < eps) return 0;
return (d > 0) ? 1 : -1;
}
int n, m, T;
int cnt[28];
string str;
int main() {
cin >> n >> str;
m = (str).length();
memset(cnt, 0, sizeof(cnt));
for (int i = 0; i < m; ++i) ++cnt[str[i] - 'a'];
for (int i = 0; i < 26; ++i) {
if (cnt[i] % n != 0) {
printf("-1\n");
return 0;
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < 26; ++j) {
for (int k = 0; k < cnt[j] / n; ++k) printf("%c", j + 'a');
}
}
printf("\n");
}
|
#include <bits/stdc++.h>
int main() {
int n, max = 0, flag = 1;
char s[1001];
scanf("%d%s", &n, &s);
int f[27] = {0};
for (int i = 0; i < strlen(s); i++) {
++f[s[i] - 96];
}
for (int i = 1; i < 27; i++) {
if (f[i] != 0 && f[i] % n != 0) {
flag = 0;
break;
}
}
if (flag == 0)
printf("%d", -1);
else {
for (int j = 0; j < n; j++) {
for (int i = 1; i < 27; i++) {
if (f[i] > 0) {
int kkk = f[i] / n;
for (int k = 0; k < kkk; k++) printf("%c", i + 96);
}
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0 && cin.tie(0) && cout.tie(0));
;
int k;
cin >> k;
string s;
cin >> s;
int arr[26] = {0};
for (int i = 0; i < s.length(); i++) {
int j = s[i] - 97;
++arr[j];
}
for (int i = 0; i < 26; i++) {
if (arr[i] % k != 0) {
cout << -1;
return 0;
}
}
string ans;
for (int i = 0; i < s.length(); i++) {
if (i == 0) {
int count = 0;
for (int j = 0; j < (arr[s[i] - 97] / k); j++) {
ans += s[i];
++count;
}
} else {
for (int j = 0; j < ans.length(); j++) {
if (s[i] == ans[j]) goto end;
}
int count = 0;
for (int x = 0; x < (arr[s[i] - 97] / k); x++) {
ans += s[i];
++count;
}
}
end:
continue;
}
for (int i = 0; i < k; i++) {
for (int j = 0; j < ans.length(); j++) {
cout << ans[j];
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int k;
cin >> k;
string str, ans = "";
cin >> str;
int arr[26];
for (int i = 0; i < 26; i++) arr[i] = 0;
for (int i = 0; i < str.length(); i++) arr[str[i] - 'a']++;
bool test = true;
for (int i = 0; i < 26; i++) {
if (arr[i] % k != 0) {
test = false;
break;
}
}
if (!test)
cout << -1;
else {
for (int i = 0; i < 26; i++) {
if (arr[i] != 0) {
arr[i] = arr[i] / k;
while (arr[i] != 0) {
ans += char(i + 'a');
arr[i]--;
}
}
}
for (int i = 0; i < k; i++) cout << ans;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
string s, m;
int n, i, a[130], j;
int main() {
cin >> n >> s;
for (; i < s.size(); i++) a[s[i]]++;
for (i = 64; i < 123; i++)
if (a[i] % n != 0) {
cout << -1;
return 0;
} else if (a[i] > 0)
for (j = 0; j < a[i] / n; j++) m += i;
for (i = 0; i < n; i++) cout << m;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int cnt[26] = {};
string s;
cin >> s;
for (char c : s) cnt[c - 'a']++;
string ans;
for (int i = 0; i < 26; i++) {
if (cnt[i] % n != 0) {
cout << "-1\n";
return 0;
}
ans += string(cnt[i] / n, i + 'a');
}
while (n--) cout << ans;
cout << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string a, b, c;
int n;
cin >> n >> a;
sort(a.begin(), a.end());
for (int i = 0; i < a.size(); i += n) b += a[i];
for (int i = 0; i < n; i++) c += b;
b = c;
sort(b.begin(), b.end());
if (b != a)
cout << -1;
else
cout << c;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
string s;
while (cin >> n >> s) {
int mc[30];
memset(mc, 0, sizeof mc);
for (int i = 0; i < s.size(); i++) mc[s[i] - 'a']++;
bool can = true;
int len = s.size();
for (int i = 0; i < s.size(); i++) can &= (mc[s[i] - 'a'] % n == 0);
if (!can)
printf("-1\n");
else {
string aux;
for (int i = 0; i < 30; i++)
if (mc[i])
for (int j = 0; j < mc[i] / n; j++) aux += (i + 'a');
for (int i = 0; i < n; i++) cout << aux;
cout << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int k;
scanf("%d", &k);
string s;
cin >> s;
if (k == 1) {
cout << s;
return 0;
}
int n = s.size();
int a[26] = {0};
for (int i = 0; i < n; i++) a[s[i] - 'a']++;
for (int i = 0; i < 26; i++) {
if (a[i] % k != 0) {
cout << -1;
return 0;
}
}
string str = "";
for (int i = 0; i < 26; i++) {
if (a[i])
for (int j = 0; j < a[i] / k; j++) str += ('a' + i);
}
for (int i = 0; i < k; i++) cout << str;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int Farr[26], k;
char arr[1005];
string ans;
int main() {
scanf("%d %s", &k, arr);
for (int i = 0; arr[i] != '\0'; i++) {
Farr[arr[i] - 'a']++;
}
for (int i = 0; i < 26; i++) {
if (Farr[i] % k != 0) {
printf("-1");
return 0;
} else
for (int j = 0; j < Farr[i] / k; j++) {
ans += static_cast<char>('a' + i);
}
}
while (k--) cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 7;
long long power(long long x, long long y) {
long long res = 1;
x = x;
while (y > 0) {
if (y & 1) res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
template <typename T>
T gcd(T a, T b) {
if (a == 0) return b;
return gcd(b % a, a);
}
template <typename T>
T powm(T a, T b, long long m) {
T cnt = 1;
while (b > 0) {
if (b % 2 == 1) cnt = (cnt * a) % m;
b /= 2;
a = (a * a) % m;
}
return cnt % m;
}
long long ncr(long long n, long long r) {
long long res = 1;
if (r > n) return 0;
if (r > n - r) r = n - r;
for (long long i = 0; i < r; i++) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
int n, k;
string s;
map<char, int> m;
void solve() {
cin >> k >> s;
n = (long long)s.size();
for (int i = 0; i < n; i++) m[s[i]]++;
string ans = "";
for (auto &it : m) {
if (it.second % k) {
cout << "-1";
return;
}
for (int j = 1; j <= it.second / k; j++) ans += it.first;
}
string ks = "";
for (int i = 1; i <= k; i++) ks += ans;
cout << ks;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
t = 1;
while (t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int freq[300];
int main() {
bool l = false;
char c;
int k, i;
string s;
string substring = "";
cin >> k >> s;
for (i = 0; i < s.length(); i++) ++freq[s[i]];
for (c = 'a'; c <= 'z'; c++) {
if (freq[c] % k != 0) {
l = true;
break;
}
for (i = 0; i < freq[c] / k; i++) substring += c;
}
if (l == false) {
while (k--) cout << substring;
} else
cout << -1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
int main() {
int k;
scanf("%d", &k);
char c;
scanf("%c", &c);
char string[1001];
gets(string);
int letter[26];
int m = 0;
for (int i = 0; i < 26; i++) {
letter[i] = 0;
}
int a = strlen(string);
for (int i = 0; i < a; i++) {
letter[string[i] - 97]++;
}
for (int i = 0; i < 26; i++) {
if (letter[i] != 0) {
if (letter[i] % k == 0) {
continue;
} else {
printf("-1");
return 0;
}
}
}
for (int o = 0; o < k; o++) {
for (int i = 0; i < 26; i++) {
for (int j = 0; j < (letter[i] / k); j++) {
printf("%c", i + 'a');
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int cnt[26];
int main() {
cin.tie(0), ios_base::sync_with_stdio(false);
int k;
string s;
cin >> k >> s;
for (auto c : s) cnt[c - 'a']++;
string ans;
for (int i = 0; i < 26; i++) {
if (cnt[i] % k) return cout << -1 << endl, 0;
for (int j = 0; j < cnt[i] / k; j++) ans.push_back('a' + i);
}
for (int i = 0; i < k; i++) cout << ans;
cout << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
struct node {
int a, b;
bool operator<(const node &n) const { return a < n.a; }
};
int n, m, k;
vector<unsigned long long> v, v2;
vector<char> vc;
string s;
map<char, int> mc;
int main() {
int t, t1;
while (cin >> n) {
cin >> s;
vc.clear();
mc.clear();
int minn = INT_MAX;
for (int i = 0; i < s.size(); i++) {
mc[s[i]]++;
if (find(vc.begin(), vc.end(), s[i]) == vc.end()) {
vc.push_back(s[i]);
}
}
for (int i = 0; i < vc.size(); i++) {
minn = min(minn, mc[vc[i]]);
}
string ans;
if (minn != 0 && minn % n == 0) {
for (int i = 0; i < vc.size(); i++) {
int cnt = mc[vc[i]] / n;
if (mc[vc[i]] % n != 0) {
cout << -1 << endl;
goto next;
}
for (int j = 0; j < cnt; j++) {
ans += vc[i];
}
}
string tmp;
for (int i = 0; i < n; i++) {
tmp += ans;
}
cout << tmp << endl;
} else
cout << -1 << endl;
next:;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
std::cout.tie(NULL);
int n, k, c, i, j;
cin >> k;
string a;
cin >> a;
c = 0;
int z[27] = {0};
int z1[27] = {0};
for (i = 0; a[i] != '\0'; i++) {
if ((int)a[i] >= 97 && (int)a[i] <= 122) {
z[(int)a[i] - 97]++;
}
}
string b = "";
for (i = 0; i < 26; i++) {
if (z[i] != 0) {
if (z[i] % k == 0)
z1[i] = z[i] / k;
else {
c = -1;
break;
}
}
}
if (c == -1)
cout << -1;
else {
for (i = 0; i < 26; i++) {
while (z1[i]--) {
b += (char)i + 97;
}
}
while (k--) cout << b;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> num(30);
int main() {
int k;
cin >> k;
string s, ans;
cin >> s;
for (int i = 0; i < s.size(); i++) num[s[i] - 'a']++;
for (int i = 0; i < 30; i++)
if (num[i] % k) {
cout << "-1" << endl;
return 0;
}
for (int i = 0; i < 30; i++)
for (int j = 0; j < num[i] / k; j++) ans.push_back('a' + i);
for (int i = 0; i < k; i++) cout << ans;
cout << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, j, f = 0;
cin >> n;
string s;
vector<char> ans;
cin >> s;
map<char, int> mp;
set<char> alp;
for (i = 0; i < s.size(); i++) {
mp[s[i]]++;
alp.insert(s[i]);
}
for (auto x : alp) {
if (mp[x] % n != 0) f++;
}
if (f != 0)
cout << -1;
else {
for (auto x : alp) {
for (i = 0; i < mp[x] / n; i++) ans.push_back(x);
}
for (j = 0; j < n; j++) {
for (i = 0; i < ans.size(); i++) cout << ans[i];
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long int a, i;
long long int ar[26] = {0};
string s, ans;
cin >> a;
cin >> s;
for (i = 0; i < s.size(); i++) {
long long int d = s[i] - 97;
ar[d]++;
}
bool ch = 1;
for (i = 0; i < 26; i++) {
if (ar[i] % a == 0)
ar[i] /= a;
else
ch = 0;
}
if (ch == 0)
cout << -1 << endl;
else {
long long int j, k;
for (j = 0; j < a; j++) {
for (i = 0; i < 26; i++) {
for (k = 0; k < ar[i]; k++) ans += (i + 97);
}
}
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int k;
cin >> k;
string str;
cin >> str;
vector<int> cnt(26, 0);
int n = str.size();
for (int i = 0; i < n; i++) {
cnt[str[i] - 'a']++;
}
for (int i = 0; i < 26; i++) {
if (cnt[i] % k != 0) {
cout << -1 << endl;
return 0;
} else {
cnt[i] = cnt[i] / k;
}
}
for (int f = 0; f < k; f++) {
for (int i = 0; i < 26; i++) {
for (int j = 0; j < cnt[i]; j++) {
cout << char(i + 97);
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
map<char, int> frequency;
string text;
int n;
cin >> n;
cin >> text;
for (int i = 0; i < text.size(); i++) {
if (frequency.find(text[i]) == frequency.end()) {
frequency[text[i]] = 1;
} else {
frequency[text[i]] += 1;
}
}
map<char, int>::iterator it = frequency.begin();
string x("");
bool error = false;
while (it != frequency.end() && !error) {
if (it->second % n != 0) {
error = true;
} else {
for (int i = 0; i < it->second / n; i++) {
x = x + it->first;
}
}
it++;
}
if (error) {
cout << "-1" << endl;
} else {
for (int i = 0; i < n; i++) {
cout << x;
}
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int k;
cin >> k;
string s;
cin >> s;
int n = s.length();
int *cnt = new int[26];
memset(cnt, 0, sizeof(cnt) * 26);
for (int i = 0; i < n; ++i) ++cnt[s[i] - 'a'];
for (int i = 0; i < 26; ++i)
if (cnt[i] % k) {
cout << -1;
return 0;
}
for (int i = 0; i < k; ++i)
for (int j = 0; j < 26; ++j)
for (int t = 0; t < cnt[j] / k; ++t) cout << (char)('a' + j);
cout << endl;
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("-ffloat-store")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int k;
cin >> k;
unordered_map<char, int> m;
string s;
cin >> s;
for (int i = 0; i < s.length(); i++) {
m[s[i]]++;
}
int flag = 0;
for (auto i : m) {
if (i.second % k != 0) {
flag = 1;
break;
}
}
string ans = "";
if (flag == 0) {
for (auto i : m) {
for (int j = 0; j < i.second / k; j++) ans += i.first;
}
for (int i = 0; i < k; i++) {
cout << ans;
}
} else
cout << "-1";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int k;
cin >> k;
string s;
cin >> s;
int a[26] = {0};
for (int i = 0; i < s.size(); i++) {
a[s[i] % 97]++;
}
string ans = "";
for (int i = 0; i < 26; i++) {
if ((a[i] % k) != 0) {
cout << -1;
return 0;
} else {
if (a[i] != 0) {
for (int j = 0; j < (a[i] / k); j++) ans += (char)(i + 97);
}
}
}
for (int i = 0; i < k; i++) cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[26] = {0};
int k;
scanf("%d", &k);
string x;
cin >> x;
int c = 0;
for (int i = 0; i < x.size(); ++i) {
arr[x[i] - 'a']++;
++c;
}
for (int i = 0; i < 25; ++i) {
if (arr[i] % k == 0) {
} else {
cout << -1;
return 0;
}
}
if (c < k || c % k != 0) {
cout << -1;
return 0;
}
for (int i = 0; i < k; ++i) {
for (int j = 0; j < 26; ++j) {
for (int y = 0; y < arr[j] / k; ++y) {
cout << (char)('a' + j);
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int k, i, j;
string str;
string ans = "";
cin >> k;
cin >> str;
int arr[26] = {0};
for (i = 0; i < str.length(); i++) arr[str[i] - 'a']++;
for (i = 0; i < 26; i++) {
if (arr[i] != 0) {
if (arr[i] % k == 0) {
for (j = 0; j < arr[i] / k; j++) ans += 'a' + i;
} else {
ans = "-1";
break;
}
}
}
if (ans == "-1")
cout << ans << endl;
else {
for (i = 0; i < k; i++) cout << ans;
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(0)->sync_with_stdio(0);
long long k;
cin >> k;
string s;
cin >> s;
long long n = s.size();
long long a[26] = {0};
for (long long i = 0; i < s.size(); i++) {
a[s[i] - 'a']++;
}
for (long long i = 0; i < 26; i++) {
if (a[i] % k != 0) {
cout << -1;
return 0;
}
}
string ans = "";
for (long long i = 0; i < 26; i++) {
for (long long j = 0; j < a[i] / k; j++) {
ans += (i + 'a');
}
}
string p = ans;
for (long long i = 0; i < k - 1; i++) {
ans += p;
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
map<unsigned int, int> letters;
string og, out = "";
int k, prevv, c = 0;
bool good = true;
int main() {
cin >> k >> og;
for (auto letter : og) {
if (letters.find(letter) == letters.end()) {
letters[letter] = 1;
} else {
letters[letter] += 1;
}
}
for (auto item : letters) {
if (item.second % k != 0) {
good = false;
}
}
if (good) {
for (auto item : letters) {
out += string((item.second / k), item.first);
}
for (int i = 0; i < k; i++) {
cout << out;
}
cout << "\n";
} else {
cout << -1 << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int k;
string s;
cin >> k >> s;
if (k == 1) {
cout << s << endl;
return 0;
} else if (s.size() % k != 0) {
cout << -1 << endl;
return 0;
}
int arr[26];
memset(arr, 0, sizeof(arr));
for (int i = 0; i < s.size(); i++) arr[s[i] - 'a']++;
bool cond = true;
string newS = "";
for (int i = 0; i < 26 && cond; i++) {
if (arr[i] % k) cond = false;
for (int counter = 0; counter < arr[i] / k; counter++) newS += 'a' + i;
}
if (!cond)
cout << -1 << endl;
else {
string ToPrint = "";
for (int counter = 0; counter < k; counter++) ToPrint += newS;
cout << ToPrint << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int k;
string s;
int cnt[100];
int main() {
while (cin >> k) {
cin >> s;
memset(cnt, 0, sizeof(cnt));
for (int i = 0; i < s.size(); i++) cnt[s[i] - 'a']++;
bool ok = 1;
string ans;
for (int i = 0; i < 26; i++) {
if (cnt[i] % k == 0) {
ans.append(cnt[i] / k, i + 'a');
} else
ok = 0;
}
if (ok) {
string a;
int t = s.size() / ans.size();
for (int i = 0; i < t; i++) a += ans;
cout << a << endl;
} else
cout << -1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
int k;
cin >> k;
string str;
cin >> str;
unordered_map<char, int> mp;
for (int i = 0; i < str.size(); i++) mp[str[i]]++;
bool t = 1;
for (auto i = mp.begin(); i != mp.end(); i++) {
if (i->second % k) t = 0;
}
if (t) {
string res = "";
for (auto i = mp.begin(); i != mp.end(); i++) {
int j = i->second / k;
while (j--) res += i->first;
}
k = str.size() / res.size();
while (k--) cout << res;
} else
cout << -1;
}
int main() {
std::ios_base::sync_with_stdio(false);
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int k;
cin >> k;
string s;
cin >> s;
int c1 = 0;
int a[26] = {0};
for (int i = 0; i < s.length(); i++) {
a[s[i] - 'a']++;
}
for (int i = 0; i < 26; i++) {
if (a[i] > 0) {
if (a[i] % k != 0) {
cout << "-1" << endl;
c1 = 1;
break;
}
}
}
string res = "";
if (c1 == 0) {
for (int i = 0; i < 26; i++) {
int temp = a[i] / k;
for (int j = 0; j < temp; j++) {
char temp = i + 97;
res += temp;
}
}
}
string res1 = "";
while (k--) {
res1 += res;
}
cout << res1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int k;
cin >> k;
int arr[26] = {0};
string st, result;
cin >> st;
bool flag = false;
for (int i = 0; i < st.length(); i++) {
arr[st[i] - 'a']++;
}
for (int j = 0; j < 26; j++) {
if (arr[j] % k != 0) {
flag = true;
break;
} else {
if (arr[j] > 0) {
for (int m = 0; m < arr[j] / k; m++) {
result += (j + 'a');
}
}
}
}
if (flag) {
cout << -1 << "\n";
} else {
for (int i = 0; i < k; i++) {
cout << result;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
set<char> ss;
string str;
long long k, j = 0, a[27] = {0};
cin >> k >> str;
for (int i = 0; i < str.size(); ++i) {
a[str[i] - 'a']++;
}
string ans;
for (int i = 0; i < 26; ++i) {
if (a[i] % k != 0)
return cout << -1, 0;
else {
for (int j = 0; j < a[i] / k; ++j) ans += i + 'a';
}
}
for (int i = 0; i < k; ++i) cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int k;
cin >> k;
string str;
cin >> str;
if (k == 1)
cout << str;
else if (str.size() % k != 0)
cout << -1;
else {
map<char, int> hist;
for (int i = 0; i < str.size(); i++) {
if (hist.count(str[i]) == 1)
hist[str[i]]++;
else
hist[str[i]] = 1;
}
bool valid = true;
for (auto& pair : hist) {
if (pair.second % k != 0) {
valid = false;
break;
}
}
if (!valid)
cout << -1;
else {
string ans = "";
while (ans.size() != str.size()) {
for (auto& pair : hist) ans += string(pair.second / k, pair.first);
}
cout << ans;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxint = -1u >> 1;
template <class T>
bool get_max(T& a, const T& b) {
return b > a ? a = b, 1 : 0;
}
template <class T>
bool get_min(T& a, const T& b) {
return b < a ? a = b, 1 : 0;
}
int k;
int num[26];
string s;
int main() {
cin >> k;
cin >> s;
memset(num, 0, sizeof(num));
for (int i = 0; i < s.size(); i++) num[s[i] - 'a']++;
int flag = 1;
for (int i = 0; i < 26; i++)
if (num[i] % k != 0) {
flag = 0;
}
if (flag == 0)
puts("-1");
else {
string ans;
for (int i = 0; i < k; i++)
for (int j = 0; j < 26; j++)
for (int g = 0; g < num[j] / k; g++) {
ans = ans + char(j + 'a');
}
cout << ans << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long x, long long y) {
if (x == 0) return y;
return (gcd(y % x, x));
}
string Tolower(string s) {
transform(s.begin(), s.end(), s.begin(), ::tolower);
return s;
}
string Toupper(string s) {
transform(s.begin(), s.end(), s.begin(), ::toupper);
return s;
}
void solve() {
long long k, l, i, j, c[26] = {0};
string s, w = "";
cin >> k;
cin >> s;
l = s.length();
for (long long i = 0; i < l; i++) {
c[s[i] - 97]++;
}
for (long long i = 0; i < l; i++) {
if (c[s[i] - 97] % k != 0 || l % k != 0) {
cout << -1;
return;
}
}
for (long long i = 0; i < 26; i++) {
if (c[i] != 0)
for (long long j = 0; j < c[i] / k; j++) w += (char)(i + 97);
}
for (long long i = 0; i < k; i++) cout << w;
return;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, j;
int k;
string s;
while (cin >> k) {
j = 0;
cin >> s;
sort(s.begin(), s.end());
for (i = 0; i < s.length(); i++) {
if (i % k == 0) j = i;
if (s[i] != s[j]) break;
}
if (i < s.length() || s.length() % k)
cout << "-1";
else
for (i = 0; i < k; i++)
for (j = i; j < s.length(); j += k) cout << s[j];
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); }
int cnt[30];
char s[1010];
const int MAXN = 1010;
vector<int> ans;
int main() {
int K;
while (cin >> K >> s) {
memset(cnt, 0, sizeof(cnt));
int len = strlen(s);
for (int i = 0; i < len; i++) cnt[s[i] - 'a']++;
bool flag = false;
for (int i = 0; i < 26; i++) {
if (cnt[i] == 0) continue;
if (cnt[i] >= K && cnt[i] % K == 0)
continue;
else
flag = true;
}
if (flag)
puts("-1");
else {
int tot = 0;
char tmp[MAXN];
int leap = 0;
while (tot < len) {
for (int i = leap; i < 26; i++) {
if (cnt[i]) {
for (int j = 0; j < cnt[i] / K; j++) tmp[tot++] = i + 'a';
}
}
leap = 0;
}
tmp[tot] = '\0';
printf("%s\n", tmp);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
string a;
int k, i, arr[26];
memset(arr, 0, sizeof(arr));
cin >> k >> a;
for (i = 0; i < a.size(); i++) {
arr[a[i] - 'a']++;
}
for (i = 0; i < 26; i++) {
if (arr[i] > 0 && arr[i] % k) {
cout << "-1\n";
return 0;
}
arr[i] /= k;
}
char temp;
while (k--) {
for (i = 0; i < 26; i++) {
temp = 'a';
if (arr[i]) {
temp += i;
for (int j = 0; j < arr[i]; j++) cout << temp;
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxint = numeric_limits<int>::max();
const long long maxll = numeric_limits<long long>::max();
const int arr = 2e5 + 10;
const int ar = 2e3 + 10;
const long double pi = acos(-1);
const long long md = 1e9 + 7;
const long double eps = 1e-6;
int cnt[ar];
int main() {
int k;
string s;
cin >> k >> s;
if (s.length() % k != 0) return cout << -1, 0;
for (auto i : s) cnt[i]++;
string res = "";
for (char i = 'a'; i <= 'z'; i++) {
if (cnt[i] % k != 0) return cout << -1, 0;
for (int j = 0; j < cnt[i] / k; j++) res += i;
}
string ans = "";
for (int i = 0; i < k; i++) ans += res;
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int k, n, i, c = 0, j;
string s, str;
map<char, int> mp;
cin >> k;
cin >> s;
sort(s.begin(), s.end());
for (i = 0; i < s.size(); i++) {
mp[s[i]]++;
}
n = s.size();
for (char ch = 'a'; ch <= 'z'; ch++) {
if (mp[ch] >= 1 && mp[ch] % k != 0) {
cout << -1;
return 0;
}
}
while (c < n) {
for (char ch = 'a'; ch <= 'z' && c < n; ch++) {
if (mp[ch] >= 1) {
for (j = 1; j <= mp[ch] / k && c < n; j++) {
cout << ch;
c++;
}
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int k, i, status, p, j;
map<char, int> m;
map<char, int>::iterator it;
string s, a;
cin >> k;
cin >> s;
for (i = 0; i < s.length(); i++) {
m[s[i]]++;
}
p = 0;
status = 1;
for (it = m.begin(); it != m.end(); it++) {
if (it->second % k == 0) {
for (i = p; i < p + ((it->second) / k); i++) {
a[i] = it->first;
}
p = i;
} else {
status = 0;
break;
}
}
a[p] = '\0';
if (status == 1) {
for (i = 0; i < k; i++) {
for (j = 0; j < p; j++) cout << a[j];
}
exit(0);
} else
cout << "-1";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int cnt[26] = {0}, k;
string s;
cin >> k >> s;
for (int i{}; i < s.size(); i++) {
cnt[s[i] - 'a']++;
}
string res{};
for (int i{}; i < 26; i++) {
if (cnt[i] % k) {
cout << -1 << '\n';
return 0;
}
if (cnt[i])
for (int j{}; j < cnt[i] / k; j++) res += char('a' + i);
}
for (int i{}; i < k; i++) cout << res;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int k;
string s, ans = "";
cin >> k >> s;
map<char, int> mp;
for (int i = 0; i < s.size(); i++) mp[s[i]]++;
for (auto i : mp) {
if (i.second % k != 0) {
cout << "-1";
return 0;
}
ans.resize(ans.size() + i.second / k, i.first);
}
for (int i = 0; i < k; i++) cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
unordered_map<char, int> mm;
int main() {
string str = "abcdefghijklmnopqrstuvwxyz";
string s;
string p;
int i, j, k, l, len, temp, d;
cin >> k;
cin >> s;
len = s.size();
int flag = 1;
for (i = 0; i < len; i++) mm[s[i]]++;
for (i = 0; i < 26; i++) {
if (mm[str[i]] != 0) {
temp = mm[str[i]];
if (temp % k == 0) {
d = temp / k;
while (d--) p += str[i];
} else {
flag = 0;
break;
}
}
}
if (flag == 0)
cout << -1 << endl;
else {
while (k--) cout << p;
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
string s;
cin >> k;
cin >> s;
vector<char> v;
if (k == 1) {
cout << s << endl;
return 0;
}
if (k > s.size()) {
cout << -1 << endl;
return 0;
}
int f[27] = {0};
for (int i = 0; i < s.size(); i++) {
f[s[i] - 96]++;
}
int fl = 1;
for (int i = 1; i <= 26; i++) {
if (f[i] % k == 0) {
continue;
} else {
cout << -1 << endl;
return 0;
}
}
for (int i = 1; i <= 26; i++) {
if (f[i] > 0) {
int h = f[i] / k;
while (h != 0) {
v.push_back(i + 96);
h--;
}
f[i] = 0;
}
}
while (k--) {
for (int i = 0; i < v.size(); i++) {
cout << v[i];
}
}
cout << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int k, key, l;
char s[1020], res[1020];
int a[30];
while (cin >> k) {
cin >> s;
l = strlen(s);
memset(a, 0, sizeof(a));
for (int i = 0; i < l; i++) {
key = s[i] - 'a';
a[key]++;
}
int kk = 1, len = 0;
for (int i = 0; i < 26; i++) {
if (a[i] % k == 0) {
for (int j = 0; j < a[i] / k; j++) {
res[len++] = 'a' + i;
}
} else {
kk = 0;
break;
}
}
res[len] = 0;
if (kk == 0) {
cout << "-1" << endl;
} else {
for (int i = 0; i < k; i++) printf("%s", res);
cout << endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, j, c[30];
memset(c, 0, sizeof(c));
string a;
cin >> n >> a;
for (i = 0; i < a.size(); i++) {
c[a[i] - 'a']++;
}
int flag = 0;
for (i = 0; i < 26; i++) {
if (c[i] > 0) {
if (c[i] % n != 0) {
flag = 1;
break;
}
}
}
if (flag == 1) {
cout << -1 << endl;
return 0;
}
int p = n;
while (n--) {
for (i = 0; i < 26; i++) {
if (c[i] > 0) {
int y = c[i] / p;
while (y--) {
char x = 'a' + i;
cout << x;
}
}
}
}
cout << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
string s, k;
cin >> s;
map<char, int> m;
map<char, int>::iterator it;
for (int i = 0; i < s.length(); i++) {
m[s[i]]++;
}
string result;
for (it = m.begin(); it != m.end(); it++) {
if (it->second % n != 0) {
cout << -1 << endl;
return 0;
} else {
int z = it->second / n;
while (z--) k.push_back(it->first);
}
}
while (n--) {
cout << k;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int k, a[150], i = 0;
for (i = 97; i <= 122; i++) a[i] = 0;
cin >> k;
char s[1005], b[1005];
scanf("%s", s);
i = 0;
while (s[i] != '\0') {
a[(int)s[i]]++;
i++;
}
int l = strlen(s);
if (l % k != 0) {
cout << -1;
return 0;
} else {
int j, m = 0, x;
for (j = 97; j < 123; j++) {
if (a[j] != 0) {
if (a[j] % k != 0) {
cout << -1;
return 0;
} else {
i = a[j] / k;
for (x = m; x < m + i; x++) {
b[x] = (char)j;
}
m = m + i;
}
}
}
b[m] = '\0';
if (k * m != l) {
cout << -1;
return 0;
} else
i = l / m;
strcpy(s, b);
for (j = 1; j < i; j++) strcat(s, b);
puts(s);
return 0;
}
}
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:36777216")
using namespace std;
inline int MAX(int a, int b) { return (a > b) ? (a) : (b); }
inline int MIN(int a, int b) { return (a < b) ? (a) : (b); }
int main() {
int const sz = 100;
int i, j, a[26], b, k;
string s;
cin >> k >> s;
int l = s.length();
for (i = 0; i <= 25; i++) a[i] = 0;
for (i = 0; i <= l - 1; i++) a[s[i] - 'a']++;
for (i = 0; i <= 25; i++)
if (a[i] % k != 0) {
cout << "-1";
return 0;
} else
a[i] /= k;
string pat = "";
for (i = 0; i <= 25; i++)
if (a[i])
for (j = 1; j <= a[i]; j++) pat += 'a' + i;
for (i = 1; i <= k; i++) cout << pat;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
static int k;
static string s;
static map<char, int> hashs;
int main() {
while (cin >> k >> s) {
hashs.clear();
for (int i = 0; i < s.length(); i++) hashs[s[i]]++;
string ans = "";
bool flag = true;
int div = -1;
for (auto it = hashs.begin(); it != hashs.end(); ++it) {
if (it->second % k != 0) {
flag = false;
break;
} else {
for (int i = 0; i < it->second / k; i++) ans += it->first;
}
}
if (!flag) {
printf("-1\n");
} else {
for (int i = 0; i < k; i++) cout << ans;
cout << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int f[27];
int main() {
int i, j, k, n, m, x, flag = 1, y[1001], o = 0, w;
char c[1001];
scanf("%d%*c", &k);
gets(c);
n = strlen(c);
for (i = 0; i < n; i++) f[c[i] - 'a']++;
int min = 2000;
if (n % k != 0) {
flag = 0;
}
for (i = 0; i < 26; i++) {
if (f[i] && f[i] < min) {
x = i;
min = f[i];
}
if (f[i]) {
o++;
y[o] = i;
}
}
if (min < k) {
flag = 0;
}
for (i = 0; i < 26; i++)
if (f[i]) {
if (f[i] % k != 0) {
flag = 0;
break;
}
}
if (!flag) {
printf("-1\n");
} else {
for (i = 1; i <= k; i++) {
for (j = 1; j <= o; j++)
for (w = 1; w <= f[y[j]] / k; w++) printf("%c", y[j] + 'a');
}
puts("");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
map<char, int> myMap;
string word;
int k;
cin >> k;
cin >> word;
for (int i = 0; i < word.size(); i++) {
myMap[word[i]]++;
}
bool ok = true;
map<char, int>::iterator iter;
for (iter = myMap.begin(); iter != myMap.end(); iter++) {
if (iter->second % k != 0) {
ok = false;
break;
}
}
string temp = "";
if (ok) {
for (iter = myMap.begin(); iter != myMap.end(); iter++) {
int x = iter->second / k;
for (int i = 0; i < x; i++) temp += iter->first;
}
for (int i = 0; i < k; i++) cout << temp;
} else {
cout << "-1";
}
cout << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
bool isPrime(int n) {
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) {
return false;
}
}
if (n == 1) return false;
return true;
}
int main() {
int ch[28] = {0};
string x;
int n;
cin >> n >> x;
for (int i = 0; i < x.length(); i++) {
ch[x[i] - 'a']++;
}
for (int i = 0; i < x.length(); i++) {
if (ch[x[i] - 'a'] % n != 0) {
cout << -1;
return 0;
}
}
for (int i = 0; i < n; i++) {
for (int c = 0; c < 28; c++) {
for (int y = 0; y < ch[c] / n; y++) {
char s = c + 'a';
cout << s;
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
string repeat(string s, int K) {
string r = "";
for (int x = 0; x < K; x++) {
r += s;
}
return r;
}
int main() {
int K;
cin >> K;
string s;
cin >> s;
int fr[26];
memset(fr, 0, sizeof(fr));
for (int x = 0; x < s.length(); x++) fr[s[x] - 'a']++;
bool p = 1;
string rep = "";
for (int x = 0; x < 26; x++) {
if (fr[x] % K == 0) {
rep += string(fr[x] / K, (char)(x + 'a'));
} else {
p = false;
}
}
if (p)
cout << repeat(rep, K) << endl;
else
cout << -1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int k, freq[27], resp = 1, repet, cont_aux = 0;
string s;
char* aux = new char[1001];
cin >> k;
cin >> s;
for (int i = 0; i < 27; i++) {
freq[i] = 0;
}
for (int i = 0; i < s.length(); i++) {
freq[s[i] - 96]++;
}
for (int i = 1; i < 27; i++) {
if (freq[i] % k != 0) {
resp = 0;
break;
}
}
if (resp == 0)
cout << "-1";
else {
for (int i = 1; i < 27; i++) {
repet = freq[i] / k;
for (int j = cont_aux; j < cont_aux + repet; j++) {
aux[j] = i + 96;
}
cont_aux = cont_aux + repet;
}
}
for (int i = 0; i < k; i++) {
for (int j = 0; j < cont_aux; j++) {
cout << aux[j];
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int k;
cin >> k;
string str;
cin >> str;
vector<int> freq(26);
for (auto x : str) freq[x - 'a']++;
string fin = "";
for (int i = 0; i < 26; ++i) {
if (freq[i] % k != 0) {
cout << -1 << "\n";
return 0;
} else if (freq[i] != 0)
fin += string(freq[i] / k, i + 'a');
}
for (int i = 0; i < k; ++i) cout << fin;
cout << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int x;
while (cin >> x) {
map<char, int> p1;
string str, strr;
;
bool l = 0;
int sum;
cin >> str;
for (int i = 0; i < str.size(); i++) {
p1[str[i]]++;
}
for (int i = 0; i < str.size(); i++) {
if (p1[str[i]] % x != 0) {
l = 1;
break;
}
}
if (l == 0) {
for (char i = 'a'; i <= 'z'; i++) {
sum = 0;
if (p1[i] > 0) {
sum = p1[i] / x;
}
while (sum--) {
strr += i;
}
}
while (x--) {
cout << strr;
}
} else
cout << -1;
cout << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
char str[100001];
int mao[50];
int main() {
int i, j, k, t, len, flag;
memset(str, 0, sizeof(str));
cin >> t >> str;
len = strlen(str);
for (i = 0; i < len; i++) mao[str[i] - 'a']++;
flag = 0;
for (i = 0; i < 30; i++)
if (mao[i] != 0) {
if (mao[i] % t != 0) {
flag = 1;
break;
}
}
if (flag == 1)
cout << -1 << endl;
else {
for (i = 0; i < t; i++)
for (j = 0; j < 50; j++) {
if (mao[j] != 0) {
for (k = 0; k < mao[j] / t; k++) printf("%c", j + 'a');
}
}
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string str;
int arr[26] = {0}, arrindex[26];
char str2[1000];
char arr2[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
int count = 0, count2 = 0, n, size, x;
bool flag = true;
cin >> n >> str;
size = str.size();
for (int i = 0; i < 26; i++) {
for (int j = 0; j < size; j++) {
if (str[j] == arr2[i]) {
arr[i]++;
}
}
}
for (int j = 0; j < 26; j++) {
for (int i = 0; i < size; i++) {
if (str[i] == arr2[j]) {
str2[count] = arr2[j];
arr2[j] = '*';
count++;
break;
}
}
}
for (int j = 0; j < 26; j++) {
if (arr[j] > 0) {
arrindex[count2] = arr[j];
count2++;
}
}
for (int j = 0; j < count2; j++) {
if (arrindex[j] % n == 0) {
continue;
} else {
flag = false;
break;
}
}
if (!flag) {
cout << -1 << endl;
return 0;
} else {
for (int j = 0; j < n; j++) {
for (int i = 0; i < count2; i++) {
x = arrindex[i];
for (int k = 0; k < x / n; k++) cout << str2[i];
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const int MX = 1e7 + 123;
ll arr[MX];
int brr[MX];
int crr[MX];
void inout() {}
void solve() {
int x;
int n;
cin >> n;
string s, b, c;
cin >> s;
sort(s.begin(), s.end());
for (int i = 0; i < s.size(); i += n) {
b += s[i];
}
for (int i = 0; i < n; i++) {
c += b;
}
b = c;
sort(b.begin(), b.end());
if (b != s)
cout << "-1" << '\n';
else
cout << c << '\n';
}
int main() {
inout();
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
string s;
int k, num[26];
int main() {
cin >> k >> s;
for (int i = 0; i < (int)s.size(); i++) num[s[i] - 'a']++;
for (int i = 0; i < 26; i++)
if (num[i] % k != 0) {
cout << -1 << endl;
return 0;
}
for (int i = 0; i < k; i++)
for (int j = 0; j < 26; j++)
for (int l = 0; l < num[j] / k; l++) cout << (char)(j + 'a');
cout << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, a[27] = {0}, c = 0;
string s, r;
cin >> n >> s;
for (int i = 0; i < s.size(); i++) a[s[i] - 'a'] += 1;
for (int i = 0; i < 27; i++)
if (a[i] % n && a[i] != 0) c++;
if (c)
cout << -1;
else {
c = 0;
for (int i = 0; i < 27; i++)
if (a[i]) {
c = a[i] / n;
while (c--) r += i + 'a';
}
while (n--) cout << r;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
char d[2001];
int l, k;
int a[201];
int b[201];
int main() {
int i, j, xx, p;
memset(a, 0, sizeof(int) * 201);
memset(b, 0, sizeof(int) * 201);
cin >> k;
cin >> d;
l = strlen(d);
for (i = 0; i < l; i++) {
xx = d[i] - 'a';
a[xx]++;
}
for (i = 0; i <= 25; i++) {
if (a[i] != 0) {
if (a[i] % k != 0) {
break;
}
b[i] = a[i] / k;
}
}
if (i <= 25) {
cout << "-1" << endl;
return 0;
}
for (i = 1; i <= k; i++) {
for (j = 0; j <= 25; j++) {
for (p = 1; p <= b[j]; p++) {
cout << (char)(j + 'a');
}
}
}
cout << endl;
return 0;
}
|
#include <bits/stdc++.h>
const int md = 1000000007;
using namespace std;
int cnt[26];
int main() {
for (int i = 0; i < 26; i++) cnt[i] = 0;
int k;
scanf("%d\n", &k);
char c;
while ((c = getchar()) != '\n' && c != EOF) cnt[c - 'a']++;
for (int i = 0; i < 26; i++)
if (cnt[i] % k != 0) {
cout << "-1" << endl;
return 0;
}
for (int i = 0; i < k; i++) {
for (int j = 0; j < 26; j++) {
for (int z = 0; z < cnt[j] / k; z++) putchar('a' + j);
}
}
putchar(10);
}
|
#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int ddx[] = {1, 0};
int ddy[] = {1, 1};
int n;
string vec[1500];
string s, ans;
int alf[1500];
int main() {
cin >> n;
cin >> s;
if (n == 1) {
cout << s << endl;
return 0;
}
for (int i = 0; i < int(s.size()); i++) {
alf[s[i]]++;
}
int aux = 0;
for (int i = 0; i < 1500; i++) {
if (alf[i] != 0 and aux == 0) aux = alf[i];
if (alf[i] != 0 and alf[i] % n != 0) {
cout << -1 << endl;
return 0;
}
}
for (int i = 0; i < 1500; i++) {
if (alf[i] != 0)
for (int j = 0; j < alf[i] / n; j++) ans += (char)i;
}
for (int i = 0; i < n; i++) cout << ans;
cout << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int alpha[27];
memset(alpha, 0, sizeof(alpha));
int k;
cin >> k;
string z;
cin >> z;
for (int i = 0; i < z.length(); i++) {
alpha[(z[i] - 'a') + 1]++;
}
vector<int> allp;
for (int i = 0; i < 27; i++) {
if (alpha[i] != 0) {
allp.push_back(alpha[i]);
}
}
for (int i = 0; i < allp.size(); i++) {
if (allp[i] % k != 0) {
cout << "-1";
return 0;
}
}
for (int w = 1; w <= k; w++) {
for (int i = 1; i <= 26; i++) {
for (int j = 1; j <= alpha[i] / k; j++) {
cout << (char)(i + 'a' - 1);
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int i, j, n;
cin >> n;
string s;
bool f = 0;
cin >> s;
if (s.size() % n != 0)
cout << -1 << endl;
else {
map<char, long long int> mm;
for (int i = 0; i < s.size(); i++) {
mm[s[i]]++;
}
map<char, long long int>::iterator it;
for (it = mm.begin(); it != mm.end(); it++) {
if (it->second % n != 0) {
f = 1;
cout << -1 << endl;
break;
}
}
if (f == 0) {
string a = "";
for (it = mm.begin(); it != mm.end(); it++) {
int c = it->second / n;
while (c) {
a += it->first;
c--;
}
}
while (n--) {
cout << a;
}
cout << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:36777216")
template <class T>
inline T &RD(T &);
template <class T>
inline void OT(const T &);
inline long long RD() {
long long x;
return RD(x);
}
inline char &RC(char &c) {
scanf(" %c", &c);
return c;
}
inline char RC() {
char c;
return RC(c);
}
inline double &RF(double &x) {
scanf("%lf", &x);
return x;
}
inline double RF() {
double x;
return RF(x);
}
inline char *RS(char *s) {
scanf("%s", s);
return s;
}
template <class T0, class T1>
inline T0 &RD(T0 &x0, T1 &x1) {
RD(x0), RD(x1);
return x0;
}
template <class T0, class T1, class T2>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2) {
RD(x0), RD(x1), RD(x2);
return x0;
}
template <class T0, class T1, class T2, class T3>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3) {
RD(x0), RD(x1), RD(x2), RD(x3);
return x0;
}
template <class T0, class T1, class T2, class T3, class T4>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4) {
RD(x0), RD(x1), RD(x2), RD(x3), RD(x4);
return x0;
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) {
RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5);
return x0;
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline T0 &RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6) {
RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6);
return x0;
}
template <class T0, class T1>
inline void OT(const T0 &x0, const T1 &x1) {
OT(x0), OT(x1);
}
template <class T0, class T1, class T2>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2) {
OT(x0), OT(x1), OT(x2);
}
template <class T0, class T1, class T2, class T3>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3) {
OT(x0), OT(x1), OT(x2), OT(x3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
const T4 &x4) {
OT(x0), OT(x1), OT(x2), OT(x3), OT(x4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
const T4 &x4, const T5 &x5) {
OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3,
const T4 &x4, const T5 &x5, const T6 &x6) {
OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6);
}
inline char &RC(char &a, char &b) {
RC(a), RC(b);
return a;
}
inline char &RC(char &a, char &b, char &c) {
RC(a), RC(b), RC(c);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d) {
RC(a), RC(b), RC(c), RC(d);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e) {
RC(a), RC(b), RC(c), RC(d), RC(e);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e, char &f) {
RC(a), RC(b), RC(c), RC(d), RC(e), RC(f);
return a;
}
inline char &RC(char &a, char &b, char &c, char &d, char &e, char &f, char &g) {
RC(a), RC(b), RC(c), RC(d), RC(e), RC(f), RC(g);
return a;
}
inline double &RF(double &a, double &b) {
RF(a), RF(b);
return a;
}
inline double &RF(double &a, double &b, double &c) {
RF(a), RF(b), RF(c);
return a;
}
inline double &RF(double &a, double &b, double &c, double &d) {
RF(a), RF(b), RF(c), RF(d);
return a;
}
inline double &RF(double &a, double &b, double &c, double &d, double &e) {
RF(a), RF(b), RF(c), RF(d), RF(e);
return a;
}
inline double &RF(double &a, double &b, double &c, double &d, double &e,
double &f) {
RF(a), RF(b), RF(c), RF(d), RF(e), RF(f);
return a;
}
inline double &RF(double &a, double &b, double &c, double &d, double &e,
double &f, double &g) {
RF(a), RF(b), RF(c), RF(d), RF(e), RF(f), RF(g);
return a;
}
inline void RS(char *s1, char *s2) { RS(s1), RS(s2); }
inline void RS(char *s1, char *s2, char *s3) { RS(s1), RS(s2), RS(s3); }
template <class T>
inline void RST(T &A) {
memset(A, 0, sizeof(A));
}
template <class T0, class T1>
inline void RST(T0 &A0, T1 &A1) {
RST(A0), RST(A1);
}
template <class T0, class T1, class T2>
inline void RST(T0 &A0, T1 &A1, T2 &A2) {
RST(A0), RST(A1), RST(A2);
}
template <class T0, class T1, class T2, class T3>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3) {
RST(A0), RST(A1), RST(A2), RST(A3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) {
RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) {
RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) {
RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6);
}
template <class T>
inline void FLC(T &A, int x) {
memset(A, x, sizeof(A));
}
template <class T0, class T1>
inline void FLC(T0 &A0, T1 &A1, int x) {
FLC(A0, x), FLC(A1, x);
}
template <class T0, class T1, class T2>
inline void FLC(T0 &A0, T1 &A1, T2 &A2) {
FLC(A0), FLC(A1), FLC(A2);
}
template <class T0, class T1, class T2, class T3>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3) {
FLC(A0), FLC(A1), FLC(A2), FLC(A3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) {
FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) {
FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4), FLC(A5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) {
FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4), FLC(A5), FLC(A6);
}
template <class T>
inline void CLR(priority_queue<T, vector<T>, less<T> > &Q) {
while (!Q.empty()) Q.pop();
}
template <class T>
inline void CLR(priority_queue<T, vector<T>, greater<T> > &Q) {
while (!Q.empty()) Q.pop();
}
template <class T>
inline void CLR(T &A) {
A.clear();
}
template <class T0, class T1>
inline void CLR(T0 &A0, T1 &A1) {
CLR(A0), CLR(A1);
}
template <class T0, class T1, class T2>
inline void CLR(T0 &A0, T1 &A1, T2 &A2) {
CLR(A0), CLR(A1), CLR(A2);
}
template <class T0, class T1, class T2, class T3>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) {
CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6);
}
template <class T>
inline void CLR(T &A, int n) {
for (int i = 0; i < int(n); ++i) CLR(A[i]);
}
template <class T>
inline void SRT(T &A) {
sort(A.begin(), A.end());
}
template <class T, class C>
inline void SRT(T &A, C B) {
sort(A.begin(), A.end(), B);
}
const int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-9;
const double OO = 1e15;
const double PI = acos(-1.0);
template <class T>
inline void checkMin(T &a, const T b) {
if (b < a) a = b;
}
template <class T>
inline void checkMax(T &a, const T b) {
if (b > a) a = b;
}
template <class T, class C>
inline void checkMin(T &a, const T b, C c) {
if (c(b, a)) a = b;
}
template <class T, class C>
inline void checkMax(T &a, const T b, C c) {
if (c(a, b)) a = b;
}
template <class T>
inline T min(T a, T b, T c) {
return min(min(a, b), c);
}
template <class T>
inline T max(T a, T b, T c) {
return max(max(a, b), c);
}
template <class T>
inline T min(T a, T b, T c, T d) {
return min(min(a, b), min(c, d));
}
template <class T>
inline T max(T a, T b, T c, T d) {
return max(max(a, b), max(c, d));
}
template <class T>
inline T sqr(T a) {
return a * a;
}
template <class T>
inline T cub(T a) {
return a * a * a;
}
inline int Ceil(int x, int y) { return (x - 1) / y + 1; }
inline bool _1(int x, int i) { return x & 1 << i; }
inline bool _1(long long x, int i) { return x & 1LL << i; }
inline long long _1(int i) { return 1LL << i; }
inline long long _U(int i) { return _1(i) - 1; };
template <class T>
inline T low_bit(T x) {
return x & -x;
}
template <class T>
inline T high_bit(T x) {
T p = low_bit(x);
while (p != x) x -= p, p = low_bit(x);
return p;
}
template <class T>
inline T cover_bit(T x) {
T p = 1;
while (p < x) p <<= 1;
return p;
}
inline int count_bits(int x) {
x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1);
x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2);
x = (x & 0x0f0f0f0f) + ((x & 0xf0f0f0f0) >> 4);
x = (x & 0x00ff00ff) + ((x & 0xff00ff00) >> 8);
x = (x & 0x0000ffff) + ((x & 0xffff0000) >> 16);
return x;
}
inline int count_bits(long long x) {
x = (x & 0x5555555555555555LL) + ((x & 0xaaaaaaaaaaaaaaaaLL) >> 1);
x = (x & 0x3333333333333333LL) + ((x & 0xccccccccccccccccLL) >> 2);
x = (x & 0x0f0f0f0f0f0f0f0fLL) + ((x & 0xf0f0f0f0f0f0f0f0LL) >> 4);
x = (x & 0x00ff00ff00ff00ffLL) + ((x & 0xff00ff00ff00ff00LL) >> 8);
x = (x & 0x0000ffff0000ffffLL) + ((x & 0xffff0000ffff0000LL) >> 16);
x = (x & 0x00000000ffffffffLL) + ((x & 0xffffffff00000000LL) >> 32);
return x;
}
inline int reverse_bits(int x) {
x = ((x >> 1) & 0x55555555) | ((x << 1) & 0xaaaaaaaa);
x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc);
x = ((x >> 4) & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0);
x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00);
x = ((x >> 16) & 0x0000ffff) | ((x << 16) & 0xffff0000);
return x;
}
inline long long reverse_bits(long long x) {
x = ((x >> 1) & 0x5555555555555555LL) | ((x << 1) & 0xaaaaaaaaaaaaaaaaLL);
x = ((x >> 2) & 0x3333333333333333LL) | ((x << 2) & 0xccccccccccccccccLL);
x = ((x >> 4) & 0x0f0f0f0f0f0f0f0fLL) | ((x << 4) & 0xf0f0f0f0f0f0f0f0LL);
x = ((x >> 8) & 0x00ff00ff00ff00ffLL) | ((x << 8) & 0xff00ff00ff00ff00LL);
x = ((x >> 16) & 0x0000ffff0000ffffLL) | ((x << 16) & 0xffff0000ffff0000LL);
x = ((x >> 32) & 0x00000000ffffffffLL) | ((x << 32) & 0xffffffff00000000LL);
return x;
}
inline void INC(int &a, int b) {
a += b;
if (a >= MOD) a -= MOD;
}
inline int sum(int a, int b) {
a += b;
if (a >= MOD) a -= MOD;
return a;
}
inline void DEC(int &a, int b) {
a -= b;
if (a < 0) a += MOD;
}
inline int dff(int a, int b) {
a -= b;
if (a < 0) a += MOD;
return a;
}
inline void MUL(int &a, int b) { a = (long long)a * b % MOD; }
inline int pdt(int a, int b) { return (long long)a * b % MOD; }
inline int sum(int a, int b, int c) { return sum(sum(a, b), c); }
inline int sum(int a, int b, int c, int d) { return sum(sum(a, b), sum(c, d)); }
inline int pdt(int a, int b, int c) { return pdt(pdt(a, b), c); }
inline int pdt(int a, int b, int c, int d) { return pdt(pdt(pdt(a, b), c), d); }
inline int pow(int a, long long b) {
int c(1);
while (b) {
if (b & 1) MUL(c, a);
MUL(a, a), b >>= 1;
}
return c;
}
template <class T>
inline T pow(T a, long long b) {
T c(1);
while (b) {
if (b & 1) c *= a;
a *= a, b >>= 1;
}
return c;
}
inline int _I(int b) {
int a = MOD, x1 = 0, x2 = 1, q;
while (true) {
q = a / b, a %= b;
if (!a) return (x2 + MOD) % MOD;
DEC(x1, pdt(q, x2));
q = b / a, b %= a;
if (!b) return (x1 + MOD) % MOD;
DEC(x2, pdt(q, x1));
}
}
inline void DIA(int &a, int b) { MUL(a, _I(b)); }
inline int qtt(int a, int b) { return pdt(a, _I(b)); }
inline int phi(int n) {
int res = n;
for (int i = 2; sqr(i) <= n; ++i)
if (!(n % i)) {
DEC(res, qtt(res, i));
do {
n /= i;
} while (!(n % i));
}
if (n != 1) DEC(res, qtt(res, n));
return res;
}
struct Po;
struct Line;
struct Seg;
inline int sgn(double x) { return x < -EPS ? -1 : x > EPS; }
inline int sgn(double x, double y) { return sgn(x - y); }
struct Po {
double x, y;
Po(double _x = 0, double _y = 0) : x(_x), y(_y) {}
friend istream &operator>>(istream &in, Po &p) { return in >> p.x >> p.y; }
friend ostream &operator<<(ostream &out, Po p) {
return out << "(" << p.x << ", " << p.y << ")";
}
friend bool operator==(Po, Po);
friend bool operator!=(Po, Po);
friend Po operator+(Po, Po);
friend Po operator-(Po, Po);
friend Po operator*(Po, double);
friend Po operator/(Po, double);
bool operator<(const Po &rhs) const {
return sgn(x, rhs.x) < 0 || sgn(x, rhs.x) == 0 && sgn(y, rhs.y) < 0;
}
Po operator-() const { return Po(-x, -y); }
Po &operator+=(Po rhs) {
x += rhs.x, y += rhs.y;
return *this;
}
Po &operator-=(Po rhs) {
x -= rhs.x, y -= rhs.y;
return *this;
}
Po &operator*=(double k) {
x *= k, y *= k;
return *this;
}
Po &operator/=(double k) {
x /= k, y /= k;
return *this;
}
double length_sqr() const { return sqr(x) + sqr(y); }
double length() const { return sqrt(length_sqr()); }
Po unit() const { return (*this) / length(); }
bool dgt() const { return !sgn(x) && !sgn(y); }
double atan() const { return atan2(y, x); }
void input() { scanf("%lf %lf", &x, &y); }
};
bool operator==(Po a, Po b) {
return sgn(a.x - b.x) == 0 && sgn(a.y - b.y) == 0;
}
bool operator!=(Po a, Po b) {
return sgn(a.x - b.x) != 0 || sgn(a.y - b.y) != 0;
}
Po operator+(Po a, Po b) { return Po(a.x + b.x, a.y + b.y); }
Po operator-(Po a, Po b) { return Po(a.x - b.x, a.y - b.y); }
Po operator*(Po a, double k) { return Po(a.x * k, a.y * k); }
Po operator*(double k, Po a) { return a * k; }
Po operator/(Po a, double k) { return Po(a.x / k, a.y / k); }
struct Line {
Po a, b;
Line(const Po &a = Po(), const Po &b = Po()) : a(a), b(b) {}
Line(const Line &l) : a(l.a), b(l.b) {}
Line(double x0, double y0, double x1, double y1)
: a(Po(x0, y0)), b(Po(x1, y1)) {}
void getequation(double, double, double) const;
Line operator+(Po x) const { return Line(a + x, b + x); }
friend ostream &operator<<(ostream &out, Line p) {
return out << p.a << "-" << p.b;
}
double length() const { return (b - a).length(); }
bool dgt() const { return (a - b).dgt(); }
void input() { a.input(), b.input(); }
};
struct Seg : Line {
Seg(const Po &a = Po(), const Po &b = Po()) : Line(a, b) {}
Seg(const Line &l) : Line(l) {}
Seg(double x0, double y0, double x1, double y1) : Line(x0, y0, x1, y1) {}
};
inline double dot(const double &x1, const double &y1, const double &x2,
const double &y2) {
return x1 * x2 + y1 * y2;
}
inline double dot(const Po &a, const Po &b) { return dot(a.x, a.y, b.x, b.y); }
inline double dot(const Po &p0, const Po &p1, const Po &p2) {
return dot(p1 - p0, p2 - p0);
}
inline double dot(const Po &o, const Line &l) { return dot(o, l.a, l.b); }
inline double dot(const Line &l, const Po &o) { return dot(o, l.a, l.b); }
inline double dot(const Line &l1, const Line &l2) {
return dot(l1.b - l1.a, l2.b - l2.a);
}
inline double det(const double &x1, const double &y1, const double &x2,
const double &y2) {
return x1 * y2 - x2 * y1;
}
inline double det(const Po &a, const Po &b) { return det(a.x, a.y, b.x, b.y); }
inline double det(const Po &p0, const Po &p1, const Po &p2) {
return det(p1 - p0, p2 - p0);
}
inline double det(const Po &o, const Line &l) { return det(o, l.a, l.b); }
inline double det(const Line &l, const Po &o) { return det(o, l.a, l.b); }
inline double det(const Line &l1, const Line &l2) {
return det(l1.b - l1.a, l2.b - l2.a);
}
void Line::getequation(double A, double B, double C) const {
A = a.y - b.y, B = b.x - a.x, C = det(a, b);
}
template <class T1, class T2>
inline double dist(const T1 &x, const T2 &y) {
return sqrt(dist_sqr(x, y));
}
template <class T1, class T2>
inline int dett(const T1 &x, const T2 &y) {
return sgn(det(x, y));
}
template <class T1, class T2>
inline int dott(const T1 &x, const T2 &y) {
return sgn(dot(x, y));
}
template <class T1, class T2, class T3>
inline int dett(const T1 &x, const T2 &y, const T3 &z) {
return sgn(det(x, y, z));
}
template <class T1, class T2, class T3>
inline int dott(const T1 &x, const T2 &y, const T3 &z) {
return sgn(dot(x, y, z));
}
template <class T1, class T2, class T3, class T4>
inline int dett(const T1 &x, const T2 &y, const T3 &z, const T4 &w) {
return sgn(det(x, y, z, w));
}
template <class T1, class T2, class T3, class T4>
inline int dott(const T1 &x, const T2 &y, const T3 &z, const T4 &w) {
return sgn(dot(x, y, z, w));
}
inline double dist_sqr(const Po &a, const Po &b) {
return sqr(a.x - b.x) + sqr(a.y - b.y);
}
inline double dist_sqr(const Po &p, const Line &l) {
Po v0 = l.b - l.a, v1 = p - l.a;
return sqr(fabs(det(v0, v1))) / v0.length_sqr();
}
inline double dist_sqr(const Po &p, const Seg &l) {
Po v0 = l.b - l.a, v1 = p - l.a, v2 = p - l.b;
if (sgn(dot(v0, v1)) * sgn(dot(v0, v2)) <= 0)
return dist_sqr(p, Line(l));
else
return min(v1.length_sqr(), v2.length_sqr());
}
inline double dist_sqr(Line l, Po p) { return dist_sqr(p, l); }
inline double dist_sqr(Seg l, Po p) { return dist_sqr(p, l); }
inline double dist_sqr(Line l1, Line l2) {
if (sgn(det(l1, l2)) != 0) return 0;
return dist_sqr(l1.a, l2);
}
inline double dist_sqr(Line l1, Seg l2) {
Po v0 = l1.b - l1.a, v1 = l2.a - l1.a, v2 = l2.b - l1.a;
double c1 = det(v0, v1), c2 = det(v0, v2);
return sgn(c1) != sgn(c2) ? 0
: sqr(min(fabs(c1), fabs(c2))) / v0.length_sqr();
}
bool isIntersect(Seg l1, Seg l2) {
if (l1.a == l2.a || l1.a == l2.b || l1.b == l2.a || l1.b == l2.b) return true;
return min(l1.a.x, l1.b.x) <= max(l2.a.x, l2.b.x) &&
min(l2.a.x, l2.b.x) <= max(l1.a.x, l1.b.x) &&
min(l1.a.y, l1.b.y) <= max(l2.a.y, l2.b.y) &&
min(l2.a.y, l2.b.y) <= max(l1.a.y, l1.b.y) &&
sgn(det(l1.a, l2.a, l2.b)) * sgn(det(l1.b, l2.a, l2.b)) <= 0 &&
sgn(det(l2.a, l1.a, l1.b)) * sgn(det(l2.b, l1.a, l1.b)) <= 0;
}
inline double dist_sqr(Seg l1, Seg l2) {
if (isIntersect(l1, l2))
return 0;
else
return min(dist_sqr(l1.a, l2), dist_sqr(l1.b, l2), dist_sqr(l2.a, l1),
dist_sqr(l2.b, l1));
}
inline bool isOnSide(const Po &p, const Seg &l) { return p == l.a || p == l.b; }
inline bool isOnSeg(const Po &p, const Seg &l) {
return sgn(det(p, l.a, l.b)) == 0 && sgn(l.a.x, p.x) * sgn(l.b.x, p.x) <= 0 &&
sgn(l.a.y, p.y) * sgn(l.b.y, p.y) <= 0;
}
inline bool isOnSegg(const Po &p, const Seg &l) {
return sgn(det(p, l.a, l.b)) == 0 && sgn(l.a.x, p.x) * sgn(l.b.x, p.x) < 0 &&
sgn(l.a.y, p.y) * sgn(l.b.y, p.y) < 0;
}
inline Po intersect(const Line &l1, const Line &l2) {
return l1.a + (l1.b - l1.a) * (det(l2.a, l1.a, l2.b) / det(l2, l1));
}
inline Po intersect(const Po &p, const Line &l) {
return intersect(Line(p, p + Po(l.a.y - l.b.y, l.b.x - l.a.x)), l);
}
inline Po rotate(Po p, double alpha, Po o = Po()) {
p.x -= o.x, p.y -= o.y;
return Po(p.x * cos(alpha) - p.y * sin(alpha),
p.y * cos(alpha) + p.x * sin(alpha)) +
o;
}
inline int rand32() {
return (bool(rand() & 1) << 30) | (rand() << 15) + rand();
}
inline int random32(int l, int r) { return rand32() % (r - l + 1) + l; }
inline int random(int l, int r) { return rand() % (r - l + 1) + l; }
int dice() { return rand() % 6; }
bool coin() { return rand() % 2; }
template <class T>
inline T &RD(T &x) {
scanf("%d", &x);
return x;
}
int ____Case;
template <class T>
inline void OT(const T &x) {
cout << x << endl;
}
const int N = 2e5;
set<pair<int, int> > vis;
map<pair<int, int>, int> step;
queue<pair<int, int> > q;
const int dir[][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1},
{0, 1}, {1, -1}, {1, 0}, {1, 1}};
pair<int, int> st, ed;
int n, a, b, r;
void solve() {
vis.clear();
step.clear();
while (!q.empty()) q.pop();
step[st] = 0;
cin >> n;
vis.insert(st);
vis.insert(ed);
while (n--) {
cin >> r >> a >> b;
for (int i = a; i <= b; ++i) vis.insert(make_pair(r, i));
}
q.push(st);
while (!q.empty()) {
pair<int, int> go, now = q.front();
int nowstep = step[now];
q.pop();
for (int d = 0; d < 8; ++d) {
go = make_pair(now.first + dir[d][0], now.second + dir[d][1]);
if (vis.find(go) != vis.end() && step.find(go) == step.end()) {
step[go] = nowstep + 1;
q.push(go);
if (go == ed) {
cout << nowstep + 1 << endl;
return;
}
}
}
}
cout << -1 << endl;
}
int main() {
while (cin >> st.first >> st.second >> ed.first >> ed.second) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
set<pair<int, int> > valid;
int d[8][2] = {{0, 1}, {-1, 1}, {-1, 0}, {-1, -1},
{0, -1}, {1, -1}, {1, 0}, {1, 1}};
int main() {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int r, a, b;
cin >> r >> a >> b;
for (int j = a; j <= b; j++) {
valid.insert({r, j});
}
}
map<pair<int, int>, int> dist;
queue<pair<int, int> > q;
q.push({x1, y1});
dist[{x1, y1}]++;
while (!q.empty()) {
pair<int, int> p = q.front();
q.pop();
for (int i = 0; i < 8; i++) {
int tr = p.first + d[i][0], tc = p.second + d[i][1];
if (tr < 1000000001 && tc < 1000000001 && valid.count({tr, tc}) &&
dist[{tr, tc}] == 0) {
if (tr == x2 && tc == y2) {
cout << dist[p];
return 0;
}
q.push({tr, tc});
dist[{tr, tc}] = dist[p] + 1;
}
}
}
cout << -1;
}
|
#include <bits/stdc++.h>
using namespace std;
bool isvalid(int x, int y) {
if ((x >= 1 && x <= 1000000000) && y >= 1 && y <= 1000000000)
return 1;
else
return 0;
}
int main() {
map<pair<int, int>, int> m;
int x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
int n;
cin >> n;
int r, a, b;
for (int i = 0; i < n; i++) {
cin >> r >> a >> b;
for (int j = a; j <= b; j++) {
if (m[make_pair(r, j)] == 0) {
m[make_pair(r, j)]++;
}
}
}
queue<pair<pair<int, int>, int> > q;
q.push(make_pair(make_pair(x0, y0), 0));
pair<pair<int, int>, int> temp;
while (!q.empty()) {
temp = q.front();
q.pop();
int i = temp.first.first;
int j = temp.first.second;
int dist = temp.second;
if (i == x1 && j == y1) {
cout << dist << endl;
return 0;
}
if (isvalid(i - 1, j - 1)) {
if (m[make_pair(i - 1, j - 1)]) {
m[make_pair(i - 1, j - 1)]--;
q.push(make_pair(make_pair(i - 1, j - 1), dist + 1));
}
}
if (isvalid(i - 1, j)) {
if (m[make_pair(i - 1, j)]) {
m[make_pair(i - 1, j)]--;
q.push(make_pair(make_pair(i - 1, j), dist + 1));
}
}
if (isvalid(i - 1, j + 1)) {
if (m[make_pair(i - 1, j + 1)]) {
m[make_pair(i - 1, j + 1)]--;
q.push(make_pair(make_pair(i - 1, j + 1), dist + 1));
}
}
if (isvalid(i, j - 1)) {
if (m[make_pair(i, j - 1)]) {
m[make_pair(i, j - 1)]--;
q.push(make_pair(make_pair(i, j - 1), dist + 1));
}
}
if (isvalid(i, j + 1)) {
if (m[make_pair(i, j + 1)]) {
m[make_pair(i, j + 1)]--;
q.push(make_pair(make_pair(i, j + 1), dist + 1));
}
}
if (isvalid(i + 1, j - 1)) {
if (m[make_pair(i + 1, j - 1)]) {
m[make_pair(i + 1, j - 1)]--;
q.push(make_pair(make_pair(i + 1, j - 1), dist + 1));
}
}
if (isvalid(i + 1, j)) {
if (m[make_pair(i + 1, j)]) {
m[make_pair(i + 1, j)]--;
q.push(make_pair(make_pair(i + 1, j), dist + 1));
}
}
if (isvalid(i + 1, j + 1)) {
if (m[make_pair(i + 1, j + 1)]) {
m[make_pair(i + 1, j + 1)]--;
q.push(make_pair(make_pair(i + 1, j + 1), dist + 1));
}
}
}
cout << -1 << endl;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.