Skip to content

Instantly share code, notes, and snippets.

@zsrinivas
Created March 9, 2015 04:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zsrinivas/1ba3acc122c780c0d559 to your computer and use it in GitHub Desktop.
Save zsrinivas/1ba3acc122c780c0d559 to your computer and use it in GitHub Desktop.
spoj IITKWPCJ - Check the string Powers, euler gcd method
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# pylint: disable=invalid-name,missing-docstring,bad-builtin
import sys
sys.stdin = open('/tmp/spojtest.in', 'r')
def main():
def eulergcd(s1, s2):
if len(s1) > len(s2):
s1, s2 = s2, s1
while s1:
if s1 == s2:
return True
if s2.startswith(s1):
s2, s1 = s1, s2[len(s1):]
else:
s1, s2 = '', ''
if len(s1) > len(s2):
s1, s2 = s2, s1
return s1
dstream = iter(sys.stdin.read().split())
for _ in range(int(next(dstream))):
print('YES' if eulergcd(next(dstream), next(dstream)) else 'NO')
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment