Codeforces 1473B - String LCM Solution
Explain:
codeforces tutorial :
We should notice that if some string is a multiple of string , then is a multiple of . This fact leads us to the conclusion that should be a common multiple of and . Since we want to minimize the length of the string , then its length is .
So we have to check that copies of the string equal to copies of the string . If such strings are equal, print them, otherwise, there is no solution.
Solution in C++:
///La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
///AH Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
- #include <bits/stdc++.h>
- using namespace std;
- int main()
- {
- int t,i,j,lcm,sll,tll;
- cin>>t;
- while(t--)
- {
- string s,t,s1,t1;
- cin>>s>>t;
- sll=s.size();
- tll=t.size();
- int lcm=(sll*tll)/__gcd(sll,tll);
- for(i=0; i<lcm/sll; i++)
- s1+=s;
- for(j=0; j<lcm/tll; j++)
- t1+=t;
- if(s1==t1)
- cout<<s1<<endl;
- else
- cout<<"-1"<<endl;
- }
- }
No comments