Submission #5992994


Source Code Expand

Copy
    #include <bits/stdc++.h>
    using namespace std;
    
    long long gcd(long long a, long long b) {
        if(b == 0)
            return a;
        else
            return gcd(b, a % b);
    }
    
    long long lcm (long long a, long long b) {
        return a / gcd(a, b) * b;
    }

    int main(){
        long long n , m ;
        string s , t;
        cin >> n >> m >> s >> t ;
        long long ans = -1;
        if(s[0] != t[0]){
            cout << ans << endl;
            return 0;
        }
        if(n == m){
            if(s == t){
                cout << n << endl;
                return 0;
            }else{
                cout << ans << endl;
            }
        }
        ans = lcm(n , m);
        long long n2 , m2;
        n2 = ans / m;
        m2 = ans / n;
        vector<long long> v1 , v2;
        for(long long i = 0 ; i < n ; i = i + n2){
            v1.push_back(s[i]);
        }
        for(long long i = 0 ; i < m ; i = i + m2){
            v2.push_back(t[i]);
        }
        for(long long i = 0; i < v1.size() ; i++){
            if(v1[i] != v2[i]){
                cout << -1 << endl;
                return 0;
            }
        }
        cout << ans << endl;
        return 0;
    }

Submission Info

Submission Time
Task A - Two Abbreviations
User okuni
Language C++14 (GCC 5.4.1)
Score 300
Code Size 1283 Byte
Status
Exec Time 7 ms
Memory 1088 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 sample-01.txt, sample-02.txt, sample-03.txt
All 300 / 300 sample-01.txt, sample-02.txt, sample-03.txt, sample-01.txt, sample-02.txt, sample-03.txt, subtask01-01.txt, subtask01-02.txt, subtask01-03.txt, subtask01-04.txt, subtask01-05.txt, subtask01-06.txt, subtask01-07.txt, subtask01-08.txt, subtask01-09.txt, subtask01-10.txt, subtask01-11.txt, subtask01-12.txt, subtask01-13.txt, subtask01-14.txt, subtask01-15.txt
Case Name Status Exec Time Memory
sample-01.txt 1 ms 256 KB
sample-02.txt 1 ms 256 KB
sample-03.txt 1 ms 256 KB
subtask01-01.txt 1 ms 256 KB
subtask01-02.txt 3 ms 384 KB
subtask01-03.txt 5 ms 512 KB
subtask01-04.txt 5 ms 512 KB
subtask01-05.txt 7 ms 1088 KB
subtask01-06.txt 6 ms 512 KB
subtask01-07.txt 2 ms 384 KB
subtask01-08.txt 4 ms 512 KB
subtask01-09.txt 5 ms 384 KB
subtask01-10.txt 6 ms 512 KB
subtask01-11.txt 6 ms 512 KB
subtask01-12.txt 5 ms 512 KB
subtask01-13.txt 6 ms 512 KB
subtask01-14.txt 6 ms 384 KB
subtask01-15.txt 6 ms 768 KB