Submission #5949731


Source Code Expand

Copy
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <utility>
#include <iomanip>
#include <cmath>
#include <set>
#include <functional>
using namespace std;
#define REP(i,n) for (int i=0;i<(n);++i)
#define rep(i,a,b) for(int i=a;i<(b);++i)
template<class T> inline bool chmin(T &a, T b){ if(a > b) { a = b; return true;} return false;}
template<class T> inline bool chmax(T &a, T b){ if(a < b) { a = b; return true;} return false;}
using ll = long long;
constexpr ll INF = 1LL << 60;
constexpr ll MOD = 1e4 + 7;

int gcd(int a,int b){
    if(b==0) return a;
    return gcd(b,a%b);
}

int main() {
    cin.tie(0); ios_base::sync_with_stdio(false);
    int N,M;cin>>N>>M;
    string S,T;cin>>S>>T;
    if(S[0]!=T[0]){
        cout << -1 << endl;
        return 0;
    }
    int g = gcd(N,M);
    ll l = N*M/g;
    // check
    bool ok = true;
    int n = l / M;
    int m = l / N;
    int s=n,t=m;
    while(s<N && t<M){
        if(S[s]==T[t]){
            s += n; t += m;
        } else {
            ok = false;
            break;
        }
    }
    cout << (ok ? l : -1) << endl;
    return 0;
}

Submission Info

Submission Time
Task A - Two Abbreviations
User tonyu0
Language C++14 (GCC 5.4.1)
Score 0
Code Size 1175 Byte
Status
Exec Time 2103 ms
Memory 640 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 sample-01.txt, sample-02.txt, sample-03.txt
All 0 / 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 1 ms 384 KB
subtask01-03.txt 1 ms 512 KB
subtask01-04.txt 1 ms 512 KB
subtask01-05.txt 2 ms 512 KB
subtask01-06.txt 1 ms 512 KB
subtask01-07.txt 1 ms 384 KB
subtask01-08.txt 1 ms 384 KB
subtask01-09.txt 1 ms 512 KB
subtask01-10.txt 2 ms 640 KB
subtask01-11.txt 2 ms 640 KB
subtask01-12.txt
subtask01-13.txt 1 ms 512 KB
subtask01-14.txt 1 ms 512 KB
subtask01-15.txt