Submission #4031451


Source Code Expand

//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <ctime>
#include <limits>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }

//math
//-------------------------------------------
template<class T> inline T sqr(T x) { return x * x; }

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define EXISTch(s,c) ((((s).find_first_of(c)) != std::string::npos)? 1 : 0)//cがあれば1 if(1)
#define SORT(c) sort((c).begin(),(c).end())

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = (int)1000000007;
const LL MOD = (LL)1000000007;//10^9+7
const LL INF2 = (LL)100000000000000000;//10^18

LL gcd(LL a, LL b) {
	if (b == 0)return a;

	gcd(b, a%b);
}

LL lcm(LL a, LL b) {
	return a / gcd(a, b)*b;
}

int main() {

	LL N, M; cin >> N >> M;
	string a, b; cin >> a >> b;


	LL L = lcm(N, M);

	map<LL, char> m;
	
	int index = 0;
	int diff = L / N;

	for (int i = 0; i < N;i++) {
		m[index] = a[i];
		index += diff;
	}

	index = 0;
	diff = L / M;

	for (int i = 0; i < M; i++) {

		if (m.find(index) != m.end()) {
			if (m[index] != b[i]) {
				cout << "-1" << endl;
				return 0;
			}
		}
		
		index += diff;
	}

	cout << L << endl;

	
	return 0;
}

Submission Info

Submission Time
Task A - Two Abbreviations
User poporo
Language C++14 (GCC 5.4.1)
Score 300
Code Size 2523 Byte
Status AC
Exec Time 45 ms
Memory 6144 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 3
AC × 21
Set Name Test Cases
Sample sample-01.txt, sample-02.txt, sample-03.txt
All 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 AC 1 ms 256 KB
sample-02.txt AC 1 ms 256 KB
sample-03.txt AC 1 ms 256 KB
subtask01-01.txt AC 1 ms 256 KB
subtask01-02.txt AC 4 ms 512 KB
subtask01-03.txt AC 22 ms 3072 KB
subtask01-04.txt AC 21 ms 2304 KB
subtask01-05.txt AC 38 ms 6144 KB
subtask01-06.txt AC 19 ms 3200 KB
subtask01-07.txt AC 8 ms 1664 KB
subtask01-08.txt AC 16 ms 2432 KB
subtask01-09.txt AC 20 ms 4096 KB
subtask01-10.txt AC 30 ms 5120 KB
subtask01-11.txt AC 45 ms 5632 KB
subtask01-12.txt AC 40 ms 5248 KB
subtask01-13.txt AC 30 ms 4992 KB
subtask01-14.txt AC 35 ms 4992 KB
subtask01-15.txt AC 25 ms 4096 KB