A - Two Abbreviations

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 300300 points

Problem Statement

You are given a string SS of length NN and another string TT of length MM. These strings consist of lowercase English letters.

A string XX is called a good string when the following conditions are all met:

  • Let LL be the length of XX. LL is divisible by both NN and MM.
  • Concatenating the 11-st, (LN+1)(LN+1)-th, (2×LN+1)(2×LN+1)-th, ......, ((N−1)×LN+1)((N−1)×LN+1)-th characters of XX, without changing the order, results in SS.
  • Concatenating the 11-st, (LM+1)(LM+1)-th, (2×LM+1)(2×LM+1)-th, ......, ((M−1)×LM+1)((M−1)×LM+1)-th characters of XX, without changing the order, results in TT.

Determine if there exists a good string. If it exists, find the length of the shortest such string.

Constraints

  • 1≤N,M≤1051≤N,M≤105
  • SS and TT consist of lowercase English letters.
  • |S|=N|S|=N
  • |T|=M|T|=M

Input

Input is given from Standard Input in the following format:

NN MM
SS
TT

Output

If a good string does not exist, print -1; if it exists, print the length of the shortest such string.


Sample Input 1 Copy

Copy

3 2
acp
ae

Sample Output 1 Copy

Copy

6

For example, the string accept is a good string. There is no good string shorter than this, so the answer is 66.


Sample Input 2 Copy

Copy

6 3
abcdef
abc

Sample Output 2 Copy

Copy

-1

Sample Input 3 Copy

Copy

15 9
dnsusrayukuaiia
dujrunuma

Sample Output 3 Copy

Copy

45

重要的坑说三遍 

long long

long long

long long
以后没啥事直接用long long

#include<bits/stdc++.h>
using namespace std;

long long gcd(long long a,long long b)
{
	 return (!b)?a:gcd(b,a%b);
}
int main()
{
	long long n,m;
	 map<long long ,long long>q;
	 cin>>n>>m;
	 int flag=0;
	 string s,t;
	 cin>>s;
	 cin>>t;
	/* if(n==n*m/gcd(n,m)||m==n*m/gcd(n,m))
	 cout<<"-1"<<endl;
	 else
	 cout<<n*m/gcd(n,m)<<endl;*/
	 long long l=n*m/gcd(n,m);
	 for(long long i=0;i<n;i++)
	 {
	 	 
	 	 	q[i*l/n+1]=s[i]-'a'+1;
		  
	 	 
	 }
	// cout<<endl;
	 
	 for(long long i=0;i<m;i++)
	 {
	 //	 cout<<i*l/m+1<<endl;
	 	 
	 	 	if(q[i*l/m+1]==t[i]-'a'+1||q[i*l/m+1]==0)
	 	 	continue;
	 	 	else
	 	 	{
	 	 		flag=1;
	 	 		break;
			  }
		  
	 }
	 if(flag)printf("-1");
	 else printf("%lld",l);
 } 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值