Sphere Online Judge

Computer geeks! Show off your skills and claim the job of your dreams! RecruitCoders.com

SPOJ Problem Set (classical)

7103. Edit Distance

Problem code: EDDIST

The edit distance of two strings S and T is the minimum number of edit operations that need to be done to transform S into T . The valid edit operations are:
• Insert a single character at any position.
• Modify an existing character.
• Remove an existing character.
For example, the edit distance of “pantera” and “aorta” is 5, because the following chain of
edits is valid (and there is no shorter chain):
“pantera” >>> “antera” >>> “aotera” >>> “aoera” >>> “aora” >>> “aorta”.

In this problem, given a value K and a word S, we need to construct a word T such that the edit distance of S and T is at most K. There are of course several possibilities for that, so we will ask that you choose the word T that comes first alphabetically. A word always comes alphabetically after any proper prefix. Among two words that are not prefixes of each other, the one that comes first alphabetically is the one that has, in the first position at wich they differ from left to right, a letter closest to the beginning of the alphabet. Notice that the empty word (that has zero characters) is a valid word and is alphabetically before any other word.

Input

The input contains several test cases. Each test case is described in a single line that contains
an integer K (0 ≤ K ≤ 1000) and a non-empty word S of at most 1200 lowercase letters,
separated by a single space. The last line of the input contains the number −1 and an asterisk
separated by a single space and should not be processed as a test case.

Output

For each test case output a single line with a word T of lowercase letters such that the edit
distance of S and T is at most K. If there are several words in that situation, output the one
that comes first alphabetically.

Example

Input:
4 abcadef
1000 zero
0 zero
-1 *
Output: aaaaaadef

zero

Added by:Pablo Ariel Heiber
Date:2010-08-13
Time limit:1s
Source limit:50000B
Languages:All except: PERL 6
Resource:FCEyN UBA ICPC Selection 2007

hide comments
2011-06-01 16:55:00 fitcat
For the second test case, should the answer be the empty word?

Anyway, the output for the second test case seems to be missing.
2010-10-09 20:19:36 Pablo Ariel Heiber
Changed the input limit to 1200. Sorry about that.
2010-10-09 20:19:36 Lordxfastx
I find out that the length of word S is greater than 1000 in the test data. Please check it.
SPOJ System © 2012 Sphere Research Labs | Projekty informatyczne i aplikacje na zamówienie. All Rights Reserved.