Two Strings
Time limit2sMemory limit512 MB
Given two digit strings s and t, find the largest difference between a number formed by a cyclic shift of s with no leading zero and a number formed by such a shift of t.
- Level
Medium7 of 10
- Topics
- String, Greedy, String matching, Implementation
- Solved
- No attempts yet
Problem
The cyclic shift of a string by positions is the string . For example, the cyclic shift of the string «abcde» by two positions is the string «cdeab». In this problem, from now on only strings consisting of the decimal digits 0 through 9 are considered. Any such string whose first character is not zero can be assigned the number whose decimal representation it is. No number is assigned to strings that begin with zero. For example, the string 123 is assigned the number one hundred twenty-three, and the string 0123 is assigned no number.
Two strings and are given. Let be the set of all cyclic shifts of the string , and the set of all cyclic shifts of the string . For example, if = «1234», then contains the strings «1234», «2341», «3412», «4123». Let be the set of numbers corresponding to the strings in the set .
Given the strings and , write a program that finds the maximum number representable as a difference , where belongs to and belongs to .
For example, if = «25» and = «12», then contains the numbers 25 and 52, and contains the numbers 12 and 21. Their pairwise differences are , , , . The maximum of these differences is 40.
Input
The first line of the input file contains the string , and the second line contains the string . Both strings are nonempty, contain only digits, at least one of which is not zero, and have length at most 3000 characters.
Output
Output the required number without leading zeros to the output file.