Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma GCC optimize("Ofast")
- #pragma GCC optimize ("unroll-loops")
- #pragma GCC optimize ("fast-math")
- //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <algorithm>
- #include <string>
- #include <cmath>
- #include <vector>
- #include <map>
- #include <set>
- #include <ctime>
- #include <stack>
- #include <queue>
- #include <iomanip>
- #include <cstdlib>
- #include <unordered_map>
- #include <unordered_set>
- #include <cstddef>
- using namespace std;
- #define forn(i, j, k) for(int i = (int)(j); i < (int)(k); i++)
- #define forn1(i, j, k) for(int i = (int)(j); i <= (int)(k); i++)
- #define pb push_back
- #define mp make_pair
- #define all(x) x.begin(),x.end()
- #define sz(a) ((int)((a).size()))
- const int INF = 1e9+1;
- const long long MOD = 1e9+7;
- typedef long long ll;
- typedef long double ld;
- ll p1[100100], p2[100100], p3[100100], step[100100], s1[100100], s2[100100], s3[100100];
- int res = INF, O[3][3];
- ll get_hash1(int l , int r) {return l != 0 ? p1[r] - p1[l - 1] * step[r - l + 1] : p1[r];}
- ll get_hash2(int l , int r) {return l != 0 ? p2[r] - p2[l - 1] * step[r - l + 1] : p2[r];}
- ll get_hash3(int l , int r) {return l != 0 ? p3[r] - p3[l - 1] * step[r - l + 1] : p3[r];}
- int32_t main()
- {
- //freopen("river.in", "r", stdin);
- //freopen("river.out", "w", stdout);
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- cout.tie(NULL);
- string a, b, c;
- cin >> a >> b >> c;
- int as = (int)a.size(), bs = (int)b.size(), cs = (int)c.size();
- int n = max(as, max(bs, cs));
- step[0] = 1;
- forn1(i, 1, n) step[i] = step[i - 1] * 31;
- p1[0] = a[0] - 'a' + 1;
- p2[0] = b[0] - 'a' + 1;
- p3[0] = c[0] - 'a' + 1;
- forn(i, 1, n){
- if(i < as) p1[i] = p1[i - 1] * 31 + a[i] - 'a' + 1;
- if(i < bs) p2[i] = p2[i - 1] * 31 + b[i] - 'a' + 1;
- if(i < cs) p3[i] = p3[i - 1] * 31 + c[i] - 'a' + 1;
- }
- s1[as - 1] = a[as - 1] - 'a' + 1;
- s2[bs - 1] = b[bs - 1] - 'a' + 1;
- s3[cs - 1] = c[cs - 1] - 'a' + 1;
- for(int i = n - 2; i >= 0; i--){
- if(i < as - 1) s1[i] = s1[i + 1] + (a[i] - 'a' + 1) * step[as - i - 1];
- if(i < bs - 1) s2[i] = s2[i + 1] + (b[i] - 'a' + 1) * step[bs - i - 1];
- if(i < cs - 1) s3[i] = s3[i + 1] + (c[i] - 'a' + 1) * step[cs - i - 1];
- }
- for(int i = 0; i < min(as, bs); i++){
- if(s1[(int)a.size() - 1 - i] == p2[i]) O[0][1] = i + 1;
- }
- for(int i = 0; i < min(as, bs); i++){
- if(s2[(int)b.size() - 1 - i] == p1[i]) O[1][0] = i + 1;
- }
- for(int i = 0; i < min(as, cs); i++){
- if(s1[(int)a.size() - 1 - i] == p3[i]) O[0][2] = i + 1;
- }
- for(int i = 0; i < min(as, cs); i++){
- if(s3[(int)c.size() - 1 - i] == p1[i]) O[2][0] = i + 1;
- }
- for(int i = 0; i < min(cs, bs); i++){
- if(s3[(int)c.size() - 1 - i] == p2[i]) O[2][1] = i + 1;
- }
- for(int i = 0; i < min(cs, bs); i++){
- if(s2[bs - 1 - i] == p3[i]) O[1][2] = i + 1;
- }
- int sum = as + bs + cs;
- res = min(res, sum - O[0][1] - O[1][2]);
- res = min(res, sum - O[0][2] - O[2][1]);
- res = min(res, sum - O[2][0] - O[1][2]);
- res = min(res, sum - O[0][2] - O[1][0]);
- res = min(res, sum - O[2][0] - O[0][1]);
- res = min(res, sum - O[2][1] - O[1][0]);
- if(as >= bs && bs >= cs){
- bool c1 = 0, c2 = 0, c3 = 0;
- for(int i = 0; i + bs - 1 < as; i++){
- if(p2[bs - 1] == get_hash1(i, i + bs - 1)){
- c1 = 1;
- break;
- }
- }
- for(int i = 0; i + cs - 1 < as; i++){
- if(p3[cs - 1] == get_hash1(i, i + cs - 1)){
- c3 = 1;
- break;
- }
- }
- for(int i = 0; i + cs - 1 < bs; i++){
- if(p3[cs - 1] == get_hash2(i, i + cs - 1)){
- c2 = 1;
- break;
- }
- }
- if(c1 && c2) res = as;
- else if(c1){
- res = min(res, as + cs - O[0][2]);
- res = min(res, as + cs - O[2][0]);
- }
- else if(c2 || c3){
- res = min(res, as + bs - O[0][1]);
- res = min(res, as + bs - O[1][0]);
- }
- } else if(as >= cs && cs >= bs){
- bool c1 = 0, c2 = 0, c3 = 0;
- for(int i = 0; i + cs - 1 < as; i++){
- if(p3[cs - 1] == get_hash1(i, i + cs - 1)){
- c1 = 1;
- break;
- }
- }
- for(int i = 0; i + bs - 1 < as; i++){
- if(p2[bs - 1] == get_hash1(i, i + bs - 1)){
- c3 = 1;
- break;
- }
- }
- for(int i = 0; i + bs - 1 < cs; i++){
- if(p2[bs - 1] == get_hash3(i, i + bs - 1)){
- c2 = 1;
- break;
- }
- }
- if(c1 && c2) res = as;
- else if(c1){
- res = min(res, as + bs - O[0][1]);
- res = min(res, as + bs - O[1][0]);
- }
- else if(c3 || c2){
- res = min(res, as + cs - O[0][2]);
- res = min(res, as + cs - O[2][0]);
- }
- } else if(bs >= as && as >= cs){
- bool c1 = 0, c2 = 0, c3 = 0;
- for(int i = 0; i + as - 1 < bs; i++){
- if(p1[as - 1] == get_hash2(i, i + as - 1)){
- c1 = 1;
- break;
- }
- }
- for(int i = 0; i + cs - 1 < bs; i++){
- if(p3[cs - 1] == get_hash2(i, i + cs - 1)){
- c3 = 1;
- break;
- }
- }
- for(int i = 0; i + cs - 1 < as; i++){
- if(p3[cs - 1] == get_hash1(i, i + cs - 1)){
- c2 = 1;
- break;
- }
- }
- if(c1 && c2) res = bs;
- else if(c1){
- res = min(res, bs + cs - O[1][2]);
- res = min(res, bs + cs - O[2][1]);
- }
- else if(c2 || c3){
- res = min(res, bs + as - O[1][0]);
- res = min(res, bs + as - O[0][1]);
- }
- } else if(bs >= cs && cs >= as){
- bool c1 = 0, c2 = 0, c3 = 0;
- for(int i = 0; i + as - 1 < bs; i++){
- if(p1[as - 1] == get_hash2(i, i + as - 1)){
- c3 = 1;
- break;
- }
- }
- for(int i = 0; i + cs - 1 < bs; i++){
- if(p3[cs - 1] == get_hash2(i, i + cs - 1)){
- c1 = 1;
- break;
- }
- }
- for(int i = 0; i + as - 1 < cs; i++){
- if(p1[as - 1] == get_hash3(i, i + as - 1)){
- c2 = 1;
- break;
- }
- }
- if(c1 && c2) res = bs;
- else if(c1){
- res = min(res, bs + as - O[1][0]);
- res = min(res, bs + as - O[0][1]);
- }
- else if(c2 || c3){
- res = min(res, bs + cs - O[1][2]);
- res = min(res, bs + cs - O[2][1]);
- }
- } else if(cs >= as && as >= bs){
- bool c1 = 0, c2 = 0, c3 = 0;
- for(int i = 0; i + as - 1 < cs; i++){
- if(p1[as - 1] == get_hash3(i, i + as - 1)){
- c1 = 1;
- break;
- }
- }
- for(int i = 0; i + bs - 1 < cs; i++){
- if(p2[bs - 1] == get_hash3(i, i + bs - 1)){
- c3 = 1;
- break;
- }
- }
- for(int i = 0; i + bs - 1 < as; i++){
- if(p2[bs - 1] == get_hash1(i, i + bs - 1)){
- c2 = 1;
- break;
- }
- }
- if(c1 && c2) res = cs;
- else if(c1){
- res = min(res, bs + cs - O[1][2]);
- res = min(res, bs + cs - O[2][1]);
- }
- else if(c2 || c3){
- res = min(res, as + cs - O[2][0]);
- res = min(res, as + cs - O[0][2]);
- }
- } else { // cs bs as
- bool c1 = 0, c2 = 0, c3 = 0;
- for(int i = 0; i + as - 1 < cs; i++){
- if(p1[as - 1] == get_hash3(i, i + as - 1)){
- c3 = 1;
- break;
- }
- }
- for(int i = 0; i + bs - 1 < cs; i++){
- if(p2[bs - 1] == get_hash3(i, i + bs - 1)){
- c1 = 1;
- break;
- }
- }
- for(int i = 0; i + as - 1 < bs; i++){
- if(p1[as - 1] == get_hash2(i, i + as - 1)){
- c2 = 1;
- break;
- }
- }
- if(c1 && c2) res = cs;
- else if(c1){
- res = min(res, as + cs - O[0][2]);
- res = min(res, as + cs - O[2][0]);
- }
- else if(c2 || c3){
- res = min(res, bs + cs - O[2][1]);
- res = min(res, bs + cs - O[1][2]);
- }
- }
- cout << res << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement