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);
 } 

猜你喜欢

转载自blog.csdn.net/explodee/article/details/83047894
two
今日推荐