[Question]
  [
Most languages come with a built-in to search a string for all occurrences of a given substring and replace those with another. I don't know of any language that generalises this concept to (not necessarily contiguous) subsequences. So that's your task in this challenge.
The input will consist of three strings `A`, `B` and `C`, where `B` and `C` are guaranteed to be of the same length. If `B` appears as a subsequence in `A` it should be replaced with `C`. Here is a simple example:
```
A: abcdefghijklmnopqrstuvwxyz
B: ghost
C: 12345
```
It would be processed like this:
```
abcdefghijklmnopqrstuvwxyz
      ||      |   ||
abcdef12ijklmn3pqr45uvwxyz
```
If there are several ways to find `B` as a subsequence, you should greedily replace the left-most one:
```
A: abcdeedcba
B: ada
C: BOB
Result:   BbcOeedcbB
and NOT:  BbcdeeOcbB
```
The same applies if `B` could be found in multiple disjoint places:
```
A: abcdeedcbaabcde
B: ed
C: 12
Result:   abcd1e2cbaabcde
and NOT:  abcd112cbaabc2e (or similar)
```
When `B` does not appear in `A`, you should output `A` unchanged.
## Rules
As stated above, take three strings `A`, `B` and `C` as input and replace the left-most occurrence of `B` as a subsequence in `A` with `C`, if there is any.
You may write a program or function, taking input via STDIN (or closest alternative), command-line argument or function argument and outputting the result via STDOUT (or closest alternative), function return value or function (out) parameter.
You may take the three strings in any consistent order which you should specify in your answer. You may assume that `B` and `C` have the same length. All strings will only contain alphanumeric characters.
Standard [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") rules apply.
## Test Cases
Each test case is four lines: `A`, `B`, `C` followed by the result.
```
abcdefghijklmnopqrstuvwxyz
ghost
12345
abcdef12ijklmn3pqr45uvwxyz
abcdeedcba
ada
BOB
BbcOeedcbB
abcdeedcbaabcde
ed
12
abcd1e2cbaabcde
121
121
aBc
aBc
abcde
acb
123
abcde
ABC
ABCD
1234
ABC
012345678901234567890123456789
42
TT
0123T5678901T34567890123456789
edcbaedcbaedcbaedcba
abcde
12345
edcbaedcbaedcbaedcba
edcbaedcbaedcbaedcbaedcba
abcde
12345
edcb1edc2aed3bae4cba5dcba
daccdedca
ace
cra
dcrcdadca
aacbcbabcccaabcbabcaabbbbca
abaaaccbac
1223334444
aacbcbabcccaabcbabcaabbbbca
aacbcbabcccaabcbabcaabbbbcac
abaaaccbac
1223334444
1ac2cb2bccc33b3bab4aa4bbbc44
```
## Leaderboard
The Stack Snippet at the bottom of this post generates leaderboards from the answers a) as a list of shortest solution per language and b) as an overall leaderboard.
To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:
```
## Language Name, N bytes
```
where `N` is the size of your submission. If you improve your score, you *can* keep old scores in the headline, by striking them through. For instance:
```
## Ruby, <s>104</s> <s>101</s> 96 bytes
```
If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the *last* number in the header:
```
## Perl, 43 + 2 (-p flag) = 45 bytes
```
You can also make the language name a link which will then show up in the snippet:
```
## [><>](http://esolangs.org/wiki/Fish), 121 bytes
```
```
/* Configuration */
var QUESTION_ID = 77719; // Obtain this from the url
// It will be like http://XYZ.stackexchange.com/questions/QUESTION_ID/... on any question page
var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";
var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk";
var OVERRIDE_USER = 8478; // This should be the user ID of the challenge author.
/* App */
var answers = [], answers_hash, answer_ids, answer_page = 1, more_answers = true, comment_page;
function answersUrl(index) {
  return "http://api.stackexchange.com/2.2/questions/" +  QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER;
}
function commentUrl(index, answers) {
  return "http://api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + COMMENT_FILTER;
}
function getAnswers() {
  jQuery.ajax({
    url: answersUrl(answer_page++),
    method: "get",
    dataType: "jsonp",
    crossDomain: true,
    success: function (data) {
      answers.push.apply(answers, data.items);
      answers_hash = [];
      answer_ids = [];
      data.items.forEach(function(a) {
        a.comments = [];
        var id = +a.share_link.match(/\d+/);
        answer_ids.push(id);
        answers_hash[id] = a;
      });
      if (!data.has_more) more_answers = false;
      comment_page = 1;
      getComments();
    }
  });
}
function getComments() {
  jQuery.ajax({
    url: commentUrl(comment_page++, answer_ids),
    method: "get",
    dataType: "jsonp",
    crossDomain: true,
    success: function (data) {
      data.items.forEach(function(c) {
        if (c.owner.user_id === OVERRIDE_USER)
          answers_hash[c.post_id].comments.push(c);
      });
      if (data.has_more) getComments();
      else if (more_answers) getAnswers();
      else process();
    }
  });  
}
getAnswers();
var SCORE_REG = /<h\d>\s*([^\n,<]*(?:<(?:[^\n>]*>[^\n<]*<\/[^\n>]*>)[^\n,<]*)*),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/;
var OVERRIDE_REG = /^Override\s*header:\s*/i;
function getAuthorName(a) {
  return a.owner.display_name;
}
function process() {
  var valid = [];
  
  answers.forEach(function(a) {
    var body = a.body;
    a.comments.forEach(function(c) {
      if(OVERRIDE_REG.test(c.body))
        body = '<h1>' + c.body.replace(OVERRIDE_REG, '') + '</h1>';
    });
    
    var match = body.match(SCORE_REG);
    if (match)
      valid.push({
        user: getAuthorName(a),
        size: +match[2],
        language: match[1],
        link: a.share_link,
      });
    else console.log(body);
  });
  
  valid.sort(function (a, b) {
    var aB = a.size,
        bB = b.size;
    return aB - bB
  });
  var languages = {};
  var place = 1;
  var lastSize = null;
  var lastPlace = 1;
  valid.forEach(function (a) {
    if (a.size != lastSize)
      lastPlace = place;
    lastSize = a.size;
    ++place;
    
    var answer = jQuery("#answer-template").html();
    answer = answer.replace("{{PLACE}}", lastPlace + ".")
                   .replace("{{NAME}}", a.user)
                   .replace("{{LANGUAGE}}", a.language)
                   .replace("{{SIZE}}", a.size)
                   .replace("{{LINK}}", a.link);
    answer = jQuery(answer);
    jQuery("#answers").append(answer);
    var lang = a.language;
    lang = jQuery('<a>'+lang+'</a>').text();
    
    languages[lang] = languages[lang] || {lang: a.language, lang_raw: lang, user: a.user, size: a.size, link: a.link};
  });
  var langs = [];
  for (var lang in languages)
    if (languages.hasOwnProperty(lang))
      langs.push(languages[lang]);
  langs.sort(function (a, b) {
    if (a.lang_raw.toLowerCase() > b.lang_raw.toLowerCase()) return 1;
    if (a.lang_raw.toLowerCase() < b.lang_raw.toLowerCase()) return -1;
    return 0;
  });
  for (var i = 0; i < langs.length; ++i)
  {
    var language = jQuery("#language-template").html();
    var lang = langs[i];
    language = language.replace("{{LANGUAGE}}", lang.lang)
                       .replace("{{NAME}}", lang.user)
                       .replace("{{SIZE}}", lang.size)
                       .replace("{{LINK}}", lang.link);
    language = jQuery(language);
    jQuery("#languages").append(language);
  }
}
```
```
body { text-align: left !important}
#answer-list {
  padding: 10px;
  width: 290px;
  float: left;
}
#language-list {
  padding: 10px;
  width: 290px;
  float: left;
}
table thead {
  font-weight: bold;
}
table td {
  padding: 5px;
}
```
```
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b">
<div id="language-list">
  <h2>Shortest Solution by Language</h2>
  <table class="language-list">
    <thead>
      <tr><td>Language</td><td>User</td><td>Score</td></tr>
    </thead>
    <tbody id="languages">
    </tbody>
  </table>
</div>
<div id="answer-list">
  <h2>Leaderboard</h2>
  <table class="answer-list">
    <thead>
      <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr>
    </thead>
    <tbody id="answers">
    </tbody>
  </table>
</div>
<table style="display: none">
  <tbody id="answer-template">
    <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr>
  </tbody>
</table>
<table style="display: none">
  <tbody id="language-template">
    <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr>
  </tbody>
</table>
```
  
[Answer]
## Python 2, 88 bytes
```
def f(a,b,c,o=""):
 for q in a:x=q==b[:1];o+=c[:x]or q;b=b[x:];c=c[x:]
 print[o,a][c>'']
```
A function that takes in the three strings and outputs the result to STDOUT. The function simply does one pass over the string, taking the appropriate char and updating `b,c` as we go.
For testing (after replacing the `print` with `return`):
```
S = """
<test cases here>
"""
for T in S.split("\n\n"):
    A,B,C,D = T.split()
    assert f(A,B,C) == D
```
[Answer]
## Java 7, 141
I think there's some more I can do with this, but I've gotta run for now. It's just a simple iterate/replace, keeping an index in A and B.
```
char[]h(char[]a,char[]b,char[]c){char[]d=a.clone();int i=0,j=0,k=b.length;for(;i<a.length&j<k;i++)if(a[i]==b[j])d[i]=c[j++];return j==k?d:a;}
```
Whitespaced for your pleasure:
```
char[]h(char[]a,char[]b,char[]c){
    char[]d=a.clone();
    int i=0,j=0,k=b.length;
    for(;i<a.length&j<k;i++)
        if(a[i]==b[j])d[i]=c[j++];
    return j==k?d:a;
}
```
[Answer]
## Lua, 121 Bytes
Straightforward solution, `gsub` allows us to iterate exactly once on each character and to replace them in a new instance of the string.
It takes input via 3 command-line argument and output a string to STDOUT.
```
a,b,c=...d=a:gsub(".",function(s)if b:find(s)then b=b:sub(2)x=c:sub(1,1)c=c:sub(2)return x end end)print(b~=''and a or d)
```
### Ungolfed
```
a,b,c=...               -- unpack the arguments into a, b and c
d=a:gsub(".",function(s)-- iterate over each character of the first argument
  if b:find(s)then      -- if the current character is in the set b
    b=b:sub(2)          -- remove it from b
    x=c:sub(1,1)        -- save the replacement character in x
    c=c:sub(2)          -- remove it from c
    return x            -- replace the current character with x
  end
end)
print(b~=''             -- if b is empty, we replaced all the character
      and a or d)       -- so output the result of gsub, else, output the first argument
```
[Answer]
# Python 3, 127 bytes.
Saved 16 bytes thanks to Katenkyo.
Still working on this a bit, man was this nastier than I thought it would be.
```
f=lambda a,b,c:a.replace(b[0],c[0],1)[:a.index(b[0])+1]+f(a[a.index(b[0])+1:],b[1:],c[1:])if b and all(x in a for x in b)else a
```
Explanation:
Awww yeah, recursion.
Test cases:
```
assert f('abcdeedcba', 'ada', 'BOB') == 'BbcOeedcbB'
assert f('abcdeedcbaabcde', 'ed', '12') == 'abcd1e2cbaabcde'
assert f('012345678901234567890123456789', '42', 'TT') == '0123T5678901T34567890123456789'
assert f('ABC', 'ABCD', '1234') == 'ABC'
```
[Answer]
# Python 3.5, 87 bytes
```
import re
lambda s,p,r:re.sub('(.*?)'.join(p),'\g<%d>'.join(r)%(*range(1,len(r)),),s,1)
```
[repl.it to verify all test cases](https://repl.it/CF26/0).
### How it works
* `'(.*?)'.join(p)` builds a search pattern that matches the subsequence to be replaced and anything between its elements.
Since the quantifiers are lazy, each `(.*?)` will match as few characters as possible.
For the pattern `ghost`, the constructed regex is `g(.*?)h(.*?)o(.*?)s(.*?)t`.
* `'\g<%d>'.join(r)%(*range(1,len(r)),)` builds the replacement string, using string formatting.
Each `\g<n>` refers to the **n**th captured group, just like `\n` would.
For the replacement `12345`, the constructed string is `1\g<1>2\g<2>3\g<3>4\g<4>5`.
* `re.sub(...,...,s,1)` performs at most one replacement in the string `s`.
[Answer]
# Pyth, 27
```
.xuXG.*HC,hSI#.nM*FxRcQ1zwQ
```
[Test Suite](http://pyth.herokuapp.com/?code=.xuXG.%2AHC%2ChSI%23.nM%2AFxRcQ1zwQ&input=%27abcdeedcbaabcde%27%0Aed%0A12&test_suite=1&test_suite_input=%27abcdefghijklmnopqrstuvwxyz%27%0Aghost%0A12345%0A%27abcdeedcba%27%0Aada%0ABOB%0A%27abcdeedcbaabcde%27%0Aed%0A12%0A%27121%27%0A121%0AaBc%0A%27abcde%27%0Aacb%0A123%0A%27ABC%27%0AABCD%0A1234%0A%27012345678901234567890123456789%27%0A42%0ATT%0A%27edcbaedcbaedcbaedcba%27%0Aabcde%0A12345%0A%27edcbaedcbaedcbaedcbaedcba%27%0Aabcde%0A12345%0A%27daccdedca%27%0Aace%0Acra&debug=0&input_size=3)
The test suite omits the last two cases because they will run out of memory. The algorithm used here is to find all of the indices of each character in the second string in the first string, then find all possible orderings of those indices and take only the ones that are in sorted order. Then use the first one of these in sorted order as the list of indices in the first string to update with values from the third string.
I feel like there should be something shorter than `.nM*F`...
[Answer]
# [MATL](https://github.com/lmendo/MATL), 33 bytes
```
y!=[]0b"@n:!<@*fX<h5Mt?}.]]?iw(}x
```
[**Try it online!**](http://matl.tryitonline.net/#code=eSE9W10wYiJAbjohPEAqZlg8aDVNdD99Ll1dP2l3KH14&input=J2FhY2JjYmFiY2NjYWFiY2JhYmNhYWJiYmJjYWMnCidhYmFhYWNjYmFjJwonMTIyMzMzNDQ0NCc)
### Explanation
```
y!      % Implicitly input first two strings. Duplicate the first and transpose
=       % Compare the two strings element-wise. Gives a 2D array with all combinations
[]      % Push empty array. Indices of matching elements will be appended to this
0       % Push a 0. This is the index of last character used up in first string
b       % Bubble up (rearrange elements in stack) to move 2D array to top
"       % For each column of that array (each char of the second string)
  @     %   Push current column
  n:!   %   Transform into column array of consecutive values starting from 1
  <     %   Compare with index of last character used up of first string
  @*    %   Push current column again. Multiply element-wise (logical AND)
  fX<   %   Find index of first matching character, or empty if there's none
  h     %   Append to array containing indices of matching elements
  5Mt   %   Push index of matching character again. Duplicate
  ?}    %   If it's empty
    .   %     Break loop
  ]     %   End if
]       % End for
        % The top of the stack now contains a copy of the index of last matching
        % character, or an empty array if there was no match
?       % If non-empty: all characters were matched
  i     %   Input third string
  w     %   Swap top two elements in stack
  (     %   Assign the characters of the third string to first string at found indices
}       % Else: the original string needs to be output
  x     %   Delete (partial) array of matching indices. Leave original string in stack
        % End if
        % Implicitly display (either modified string or original string)
```
[Answer]
# [Jelly](http://github.com/DennisMitchell/jelly), ~~23~~ ~~22~~ 21 bytes
```
='T€ŒpfṢ€$ḢṬœp³ż⁵$³Ḋ?
```
[Try it online!](http://jelly.tryitonline.net/#code=PSdU4oKsxZJwZuG5ouKCrCThuKLhuazFk3DCs8W84oG1JMKz4biKPw&input=&args=J2VkY2JhZWRjYmFlZGNiYWVkY2JhZWRjYmEn+J2FiY2RlJw+JzEyMzQ1Jw) Note that the last two test cases will run out of memory.
### Verification
```
$ head -n 5 test-cases
abcdefghijklmnopqrstuvwxyz
ghost
12345
abcdef12ijklmn3pqr45uvwxyz
$ cat subseq-short
while read s; do
        read p; read r; read o; echo $o; read
        timeout 1s jelly eun $1 "='T€ŒpfṢ€$ḢṬœp³ż⁵$³Ḋ?" "'$s'" "'$p'" "'$r'"
        (($?)) && echo '(killed)'
done < test-cases
$ ./subseq-short
abcdef12ijklmn3pqr45uvwxyz
abcdef12ijklmn3pqr45uvwxyz
BbcOeedcbB
BbcOeedcbB
abcd1e2cbaabcde
abcd1e2cbaabcde
aBc
aBc
abcde
abcde
ABC
ABC
0123T5678901T34567890123456789
0123T5678901T34567890123456789
edcbaedcbaedcbaedcba
edcbaedcbaedcbaedcba
edcb1edc2aed3bae4cba5dcba
edcb1edc2aed3bae4cba5dcba
dcrcdadca
dcrcdadca
aacbcbabcccaabcbabcaabbbbca
(killed)
1ac2cb2bccc33b3bab4aa4bbbc44
(killed)
```
### How it works
```
='T€ŒpfṢ€$ḢṬœp³ż⁵$³Ḋ?  Main link. Arguments: string s, pattern p, replacement r
='                     Compare each character of s with each character of p.
                       This yields a 2D list. Each row corresponds to a char in p.
  T€                   Compute the truthy indices of each row, i.e., the indices
                       of all occurrences of that char in s.
   Œp                  Compute the Cartesian product of the lists of indices.
        $              Combine the two links to the left into a monadic chain:
      Ṣ€                 Sort each list of indices.
     f                   Filter, removing all non-sorted lists of indices.
         Ḣ             Head; take the first (sorted) list of indices.
          Ṭ            Truth; generate a list with 1's at those indices.
           œp³         Partition; split s at all 1's, removing those characters.
                  Ḋ?   If the partition has more than more than one element:
              ż⁵$        Zip the partition with r.
                 ³       Else, return s.
```
[Answer]
# JavaScript (ES6), 84 bytes
```
(a,b,c)=>[...b].every((q,i)=>r[p=a.indexOf(q,p)]=~p++&&c[i],p=0,r=[...a])?r.join``:a
```
## Explanation / Test
```
var solution =
(a,b,c)=>
  [...b].every((q,i)=>
    r[p=a.indexOf(q,p)]=~p++&&c[i],
    p=0,
    r=[...a]
  )?r.join``
  :a
// Test
var testCases = `abcdefghijklmnopqrstuvwxyz
ghost
12345
abcdef12ijklmn3pqr45uvwxyz
abcdeedcba
ada
BOB
BbcOeedcbB
abcdeedcbaabcde
ed
12
abcd1e2cbaabcde
121
121
aBc
aBc
abcde
acb
123
abcde
ABC
ABCD
1234
ABC
012345678901234567890123456789
42
TT
0123T5678901T34567890123456789
edcbaedcbaedcbaedcba
abcde
12345
edcbaedcbaedcbaedcba
edcbaedcbaedcbaedcbaedcba
abcde
12345
edcb1edc2aed3bae4cba5dcba
daccdedca
ace
cra
dcrcdadca
aacbcbabcccaabcbabcaabbbbca
abaaaccbac
1223334444
aacbcbabcccaabcbabcaabbbbca
aacbcbabcccaabcbabcaabbbbcac
abaaaccbac
1223334444
1ac2cb2bccc33b3bab4aa4bbbc44`
.split`\n\n`.map(c=>c.split`\n`);
document.write("<pre>"+testCases.map(c=>(r=solution(c[0],c[1],c[2]))+" : "+(r==c[3]?"PASS":"FAIL")).join`\n`);
```
[Answer]
## JavaScript (ES6), ~~84~~ 76 bytes
```
(a,b,c)=>a.replace(RegExp([...b].join`(.*?)`),c.replace(/\B/g,(_,i)=>'$'+i))
```
Because I was sure that this was a job for RegExp.
Edit: Saved 8 bytes thanks to @MartinBüttner♦.
A port of @KevinLau's Ruby answer took 82 bytes:
```
([...a],[...b],[...c])=>(d=a.map(e=>e==b[0]?c.shift(b.shift()):e),b[0]?a:d).join``
```
I also tried a recursive RegExp solution but that took 90 bytes:
```
f=(a,[b,...d],[c,...e])=>b?a.replace(RegExp(b+'(.*'+d.join`.*`+'.*)'),(_,s)=>c+f(s,d,e)):a
```
[Answer]
## Julia, ~~89~~ 70 bytes
```
f(s,a,b,i=0)=(o=join(["$a "[i+1]!=c?c:b[i+=1]for c=s]);i<endof(a)?s:o)
```
Uses an index `i` to iterate through the pattern/replacement strings as we go. -19 bytes thanks to @Dennis!
[Answer]
# C, 98 bytes
```
char*f(i,o,s,r)char*i,*o,*s,*r;{char*I=i,*O=o;for(;*i;++i,++o)*o=*i==*s?++s,*r++:*i;return*s?I:O;}
```
# /\* Expanded code \*/
```
char *f(i, o, s, r)
    char *i, *o, *s, *r;
{
    char *I=i, *O=o;
    for (;  *i;  ++i,++o)
        *o = (*i==*s) ? (++s,*r++) : *i;
    return *s ? I : O;
}
```
Arguments are: **i**nput string, **o**utput buffer, **s**earch string, **r**eplacement.
After remembering the start of input and output, we walk the input, replacing and advancing the substitution whenever we hit one. At the end, if we've run out of substitutions, return the output buffer, else the unmodified input.
## /\* Tests \*/
```
struct T
{
    const char *input;
    const char *search;
    const char *replace;
    const char *expected;
};
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    int i;
    static const struct T test[] = {
        { "abcdefghijklmnopqrstuvwxyz",
          "ghost",
          "12345",
          "abcdef12ijklmn3pqr45uvwxyz"},
        { "abcdeedcba",
          "ada",
          "BOB",
          "BbcOeedcbB"},
        { "abcdeedcbaabcde",
          "ed",
          "12",
          "abcd1e2cbaabcde"},
        { "121",
          "121",
          "aBc",
          "aBc"},
        { "abcde",
          "acb",
          "123",
          "abcde"},
        { "ABC",
          "ABCD",
          "1234",
          "ABC"},
        { "012345678901234567890123456789",
          "42",
          "TT",
          "0123T5678901T34567890123456789"},
        { "edcbaedcbaedcbaedcba",
          "abcde",
          "12345",
          "edcbaedcbaedcbaedcba"},
        { "edcbaedcbaedcbaedcbaedcba",
          "abcde",
          "12345",
          "edcb1edc2aed3bae4cba5dcba"},
        { "daccdedca",
          "ace",
          "cra",
          "dcrcdadca"},
        { "aacbcbabcccaabcbabcaabbbbca",
          "abaaaccbac",
          "1223334444",
          "aacbcbabcccaabcbabcaabbbbca"},
        { "aacbcbabcccaabcbabcaabbbbcac",
          "abaaaccbac",
          "1223334444",
          "1ac2cb2bccc33b3bab4aa4bbbc44"
        }
    };
    for (i = 0;  i < (sizeof test) / (sizeof test[0]);  ++i) {
        const struct T *t = test+i;
        char *out = malloc(strlen(t->input)+1);
        char *result = f(t->input, out, t->search, t->replace);
        if (strcmp(t->expected, result))
            printf("Failed test %d; result = \"%s\"\n", i, result);
    }
    return EXIT_SUCCESS;
}
```
[Answer]
# R, 76 bytes
```
function(a,b,c){s=substr;for(x in 1:nchar(b)){a=sub(s(b,x,x),s(c,x,x),a)};a}
```
uses `sub` to replace first match
Ungolfed
```
function(a,b,c){                    # function with 3 arguments as per description
  s=substr;                         # alias for substr (saves 1 byte)
   for(x in 1:nchar(b)){            # index 1 to number character in b
     a=sub(s(b,x,x),s(c,x,x),a)};   # replace first instance of b[x] in a  
                                    # with c[x] and reassign to a
 a}                                 # return a
```
[Answer]
## C++, 204 bytes
### Golfed
```
#include<iostream>
#include<string>
int main(){std::string a, b, c;std::cin>>a>>b>>c;int t=0;for(int x=0;x<b.length();x++){t=a.find(b[x],t);if(t!=-1){a.replace(t,1,c.substr(x,1));}}std::cout<<a;return 0;}
```
### Ungolfed
```
#include<iostream>
#include<string>
int main()
{
    std::string a, b, c;
    std::cin>>a>>b>>c;
    int t = 0;
    for (int x=0;x<b.length();x++) {
        t = a.find(b[x], t);
        if (t != -1) {
            a.replace(t,1,c.substr(x, 1));
        }
    }
    std::cout<<a;
    return 0;
}
```
[Answer]
## [Retina](https://github.com/mbuettner/retina), 63 bytes
```
.+$
$&¶;$&
+`^(.)(.*¶)(.)([^;]+);(.*?)\1
$2$4$5$3;
A1`
;
G2=`.
```
Input is taken in order `B`, `C`, `A`, separated by linefeeds.
[Try it online.](http://retina.tryitonline.net/#code=LiskCiQmwrY7JCYKK2BeKC4pKC4qwrYpKC4pKFteO10rKTsoLio_KVwxCiQyJDQkNSQzOwpBMWAKOwoKRzI9YC4&input=YWJhYWFjY2JhYwoxMjIzMzM0NDQ0CmFhY2JjYmFiY2NjYWFiY2JhYmNhYWJiYmJjYWM)
[Answer]
# Haskell, 87 bytes
```
x@((a,b):c)#(d:e)|a==d,([],z)<-c#e=([],b:z)|0<1=(d:)<$>x#e
x#y=(x,y)
a!b=snd.(zip a b#)
```
I noticed the lack of a Haskell answer, and decided to fix that.
This defines a ternary function `!` with argument order pattern-replacement-string.
[Try it here.](http://rextester.com/WAP55923)
## Explanation
The auxiliary function `#` takes a list `x` of character pairs (pattern and replacement) and a string `y`.
If the "pattern" characters in `x` form a subsequence of `y`, it returns the empty list and `y` with each pattern character replaced by its counterpart.
Otherwise, it returns the pair `(x,y)`.
The function `!` zips the pattern and replacement strings into `x`, applies `#` to `x` and the third string, and returns the second component of the result.
```
x@((a,b):c)#(d:e)  -- First case of #: both arguments nonempty.
  |a==d,           -- If the pattern char matches the string's head,
   ([],z)<-c#e     -- and the pattern's tail is a subsequence of the string's tail,
  =([],b:z)        -- tack the replacement char to the recursion result.
  |0<1             -- Otherwise,
  =(d:)<$>x#e      -- recurse with the same pairs and tack string's head to result.
x#y=(x,y)          -- If either argument is empty, just pair them.
```
If the pattern is a subsequence of the string, the code runs in **O(n)** time, making one recursive pass through the string and greedily constructing the replacement in the process.
However, if the pattern is not a subsequence, it runs in **O(2n)** time in the worst case.
This is because at every position where the pattern and string have a matching character, the function calls itself to check whether the pattern is actually a subsequence, finds out it's not, and calls itself a second time to actually compute the result.
[Answer]
## JavaScript (ES6), ~~100~~ 95 bytes
```
(a,b,c)=>1?(t=[...a].map(e=>b[0]==e?(u=c[0],b=b.slice(1),c=c.slice(1),u):e).join``,b==""?t:a):a
```
This is a valid JavaScript Lambda function. Outputs as function `return`. Takes in three arguments (`a,b,c`). Add `f=` at the beginning and invoke like `f(arg1,arg2,arg3)`.
```
f=(a,b,c)=>1?(t=[...a].map(e=>b[0]==e?(u=c[0],b=b.slice(1),c=c.slice(1),u):e).join``,b==""?t:a):a
console.log(f(prompt("Value for A"),prompt("Value for B"),prompt("Value for C")))
```
[Answer]
# C (gcc), ~~67~~ ~~62~~ ~~61~~ 59 bytes
```
f(s,a,b)char*s,*a,*b;{*s==*a?*s=*b++,a++:1;*a&&f(s+1,a,b);}
```
[Try it online!](https://tio.run/nexus/c-gcc#VY45DsIwEEX7nAK5QMGeJiwNVoTEBTgAohgvJCxJANtsUc4ebKMgUc3T139f0@9TAwhiIku8UQMUgQreUpPnFFf@UMEYIGPLjFMcj32dZVHgXV/hoU4nbRLckdnucoJCKr0vysPxdK7q5nK9Gevuj@frTQBDoSgbYwmIwNl0Nl8Qngwv8OTirEmNh@@k/U1qJQUSkDFQnlSg9WYddQsS1KDbn@7@9UgEdEi1IlB8f4gLDjQUw4Lz0PUf "C (gcc) – TIO Nexus")
[Answer]
# Octave, 97 bytes
```
function A=U(A,B,C)t=0;for s=B if p=find(A(t+1:end)==s,1) D(t=p+t)=~0;else return;end;end;A(D)=C;
```
Iterate over the subsequence to replace; find first occurrence of first character, find next character in remaining string, repeat. The one interesting bit of this is:
```
D(t=p+t)=~0
D(     )      %// D is a logical mask of characters to replace in the input string
  t=p+t       %// t is the current end of D 
              %// p is the location of the character to replace
              %// update t and use as index to grow D
        =~0   %// make it so, number 1
```
Since ideone is still not accepting functions with names other than '', I'll just leave a sample run here. Inputs are only shown for the first few test cases for brevity. `key` is the expected output, `ans` is the function output.
```
A = abcdefghijklmnopqrstuvwxyz
B = ghost
C = 12345
key = abcdef12ijklmn3pqr45uvwxyz
ans = abcdef12ijklmn3pqr45uvwxyz
A = abcdeedcba
B = ada
C = BOB
key = BbcOeedcbB
ans = BbcOeedcbB
A = abcdeedcbaabcde
B = ed
C = 12
key = abcd1e2cbaabcde
ans = abcd1e2cbaabcde
key = aBc
ans = aBc
key = abcde
ans = abcde
key = ABC
ans = ABC
key = 0123T5678901T34567890123456789
ans = 0123T5678901T34567890123456789
key = edcbaedcbaedcbaedcba
ans = edcbaedcbaedcbaedcba
key = edcb1edc2aed3bae4cba5dcba
ans = edcb1edc2aed3bae4cba5dcba
key = dcrcdadca
ans = dcrcdadca
key = aacbcbabcccaabcbabcaabbbbca
ans = aacbcbabcccaabcbabcaabbbbca
key = 1ac2cb2bccc33b3bab4aa4bbbc44
ans = 1ac2cb2bccc33b3bab4aa4bbbc44
```
[Answer]
# Python 3, 123 Bytes
A different approach I wanted to share, which is a few bytes shorter. There are no rules against standard library/regex, right?
```
import re
j=''.join
m='(.*?)'
def f(A,B,C):
 *r,l=(re.findall(m+m.join(B)+'(.*)',A)or[[A]])[0]
 print(j(map(j,zip(r,C)))+l)
```
PS. This is my first golf. Let me know of any issues/improvements.
[Answer]
# Pyth, 22 bytes
```
|eJ:Ej"(.*?)"+E\$3s.iJ
```
Verify all test cases in the [Pyth Compiler](https://pyth.herokuapp.com/?code=%7CeJ%3AEj%22%28.%2a%3F%29%22%2BE%5C%243s.iJ&test_suite=1&test_suite_input=%2212345%22%0A%22abcdefghijklmnopqrstuvwxyz%22%0A%22ghost%22%0A%22BOB%22%0A%22abcdeedcba%22%0A%22ada%22%0A%2212%22%0A%22abcdeedcbaabcde%22%0A%22ed%22%0A%22aBc%22%0A%22121%22%0A%22121%22%0A%22123%22%0A%22abcde%22%0A%22acb%22%0A%221234%22%0A%22ABC%22%0A%22ABCD%22%0A%22TT%22%0A%22012345678901234567890123456789%22%0A%2242%22%0A%2212345%22%0A%22edcbaedcbaedcbaedcba%22%0A%22abcde%22%0A%2212345%22%0A%22edcbaedcbaedcbaedcbaedcba%22%0A%22abcde%22%0A%22cra%22%0A%22daccdedca%22%0A%22ace%22%0A%221223334444%22%0A%22aacbcbabcccaabcbabcaabbbbca%22%0A%22abaaaccbac%22%0A%221223334444%22%0A%22aacbcbabcccaabcbabcaabbbbcac%22%0A%22abaaaccbac%22&debug=0&input_size=3).
### Background
We build a regex from the pattern by appending a `$` and placing `(.*?)` between all characters. This regex will match the subsequence to be replaced and anything between its elements, and anything up to the end of the string.
Since the quantifiers are lazy, each `(.*?)` will match as few characters as possible.
For the pattern ghost, the constructed regex is `g(.*?)h(.*?)o(.*?)s(.*?)t(.*?)$`.
If the pattern matches the input, the builtin `r<str><regex>3` will return an array containing the prematch (everything before the subsequence), all captured groups (everything in between and after the subsequence), and the postmatch (the empty string).
If the pattern does not match, the builtin will return a singleton array containing the original input.
### How it works
```
|eJ:Ej"(.*?)"+E\$3s.iJQ  (implicit) Store the first line of input in Q.
             +E\$        Read the third line of input (pattern) and append '$'.
     j"(.*?)"            Join the result, separating by "(.*?)".
    E                    Read the third line of input (string).
   :             3       Match the string against the regex, as detailed above.
  J                      Save the returned array in J.
 e                       Extract the last element of J. This is an empty string
                         for a successful match or the original string.
|                        Logical OR; replace an empty string with the following:
                   .iJQ    Interleave J and the replacement.
                  s        Flatten the resulting array of strings.
```
[Answer]
# [Jelly](http://github.com/DennisMitchell/jelly), 23 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
Ṭœpż⁵
0ẋai1
⁴='-;ç\ñ⁴P?
```
This is two bytes longer than [my other Jelly answer](http://jelly.tryitonline.net/#code=PSdU4oKsxZJwZuG5ouKCrCThuKLhuazFk3DCs8W84oG1JMKz4biKPw&input=&args=J2VkY2JhZWRjYmFlZGNiYWVkY2JhZWRjYmEn+J2FiY2RlJw+JzEyMzQ1Jw), but it finishes instantly. [Try it online!](http://jelly.tryitonline.net/#code=4bmsxZNwxbzigbUKMOG6i2FpMQrigbQ9Jy07w6dcw7HigbRQPw&input=&args=J2FiYWFhY2NiYWMn+J2FhY2JjYmFiY2NjYWFiY2JhYmNhYWJiYmJjYWMn+JzEyMjMzMzQ0NDQn)
### Verification
```
$ head -n 5 test-cases
abcdefghijklmnopqrstuvwxyz
ghost
12345
abcdef12ijklmn3pqr45uvwxyz
$ cat subseq-fast
while read s; do
        read p; read r; read o; echo $o; read
        timeout 10s jelly eun $1 "Ṭœpż⁵¶0ẋai1¶⁴='-;ç\ñ⁴P?" "'$p'" "'$s'" "'$r'"
        (($?)) && echo '(killed)'
done < test-cases
$ ./subseq-fast
abcdef12ijklmn3pqr45uvwxyz
abcdef12ijklmn3pqr45uvwxyz
BbcOeedcbB
BbcOeedcbB
abcd1e2cbaabcde
abcd1e2cbaabcde
aBc
aBc
abcde
abcde
ABC
ABC
0123T5678901T34567890123456789
0123T5678901T34567890123456789
edcbaedcbaedcbaedcba
edcbaedcbaedcbaedcba
edcb1edc2aed3bae4cba5dcba
edcb1edc2aed3bae4cba5dcba
dcrcdadca
dcrcdadca
aacbcbabcccaabcbabcaabbbbca
aacbcbabcccaabcbabcaabbbbca
1ac2cb2bccc33b3bab4aa4bbbc44
1ac2cb2bccc33b3bab4aa4bbbc44
```
### How it works
```
⁴='-;ç\ñ⁴P?  Main link. Arguments: pattern p, string s, replacement r
⁴='          Compare each character of s with each character of p.
             This yields a 2D list. Each row corresponds to a char in p.
   -;        Prepend -1 to the 2D list, yielding a ragged array.
     ç\      Cumulatively reduce the array by the second helper link.
         P?  If the product of the resulting list is non-zero:
       ñ       Call the first helper link with the list and s as arguments.
        ⁴      Else, return s.
Ṭœpż⁵        First helper link. Arguments: L (list of indices), r (replacement)
Ṭ            Truth; generate a list with 1's at those indices.
 œp          Partition; split s at all 1's, removing those characters.
   ż⁵        Zip the partition with r.
0ẋai1        Second helper link. Arguments: n (integer), B (list of Booleans)
0ẋ           Generate a list of n zeroes.
  a          Perform logical AND with B.
             This zeroes out the with n elements of B.
   i1        Compute the first index of 1.
```
[Answer]
# CJam, 29 bytes
```
l_ll.{2$#_3$<@+\)@>}s_2$,>@@?
```
[Try it online!](http://cjam.tryitonline.net/#code=bF9sbC57MiQjXzMkPEArXClAPn1zXzIkLD5AQD8&input=YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoKMTIzNDUKZ2hvc3Q) or [verify all test cases](http://cjam.tryitonline.net/#code=ewogICAgbF9sbC57MiQjXzMkPEArXClAPn1zXzIkLD5AQD8KCiAgICBObE5sIQp9Zw&input=YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoKMTIzNDUKZ2hvc3QKYWJjZGVmMTJpamtsbW4zcHFyNDV1dnd4eXoKCmFiY2RlZWRjYmEKQk9CCmFkYQpCYmNPZWVkY2JCCgphYmNkZWVkY2JhYWJjZGUKMTIKZWQKYWJjZDFlMmNiYWFiY2RlCgoxMjEKYUJjCjEyMQphQmMKCmFiY2RlCjEyMwphY2IKYWJjZGUKCkFCQwoxMjM0CkFCQ0QKQUJDCgowMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkKVFQKNDIKMDEyM1Q1Njc4OTAxVDM0NTY3ODkwMTIzNDU2Nzg5CgplZGNiYWVkY2JhZWRjYmFlZGNiYQoxMjM0NQphYmNkZQplZGNiYWVkY2JhZWRjYmFlZGNiYQoKZWRjYmFlZGNiYWVkY2JhZWRjYmFlZGNiYQoxMjM0NQphYmNkZQplZGNiMWVkYzJhZWQzYmFlNGNiYTVkY2JhCgpkYWNjZGVkY2EKY3JhCmFjZQpkY3JjZGFkY2EKCmFhY2JjYmFiY2NjYWFiY2JhYmNhYWJiYmJjYQoxMjIzMzM0NDQ0CmFiYWFhY2NiYWMKYWFjYmNiYWJjY2NhYWJjYmFiY2FhYmJiYmNhCgphYWNiY2JhYmNjY2FhYmNiYWJjYWFiYmJiY2FjCjEyMjMzMzQ0NDQKYWJhYWFjY2JhYwoxYWMyY2IyYmNjYzMzYjNiYWI0YWE0YmJiYzQ0).
[Answer]
# Java 7, 102 bytes
```
void L(char[]s,char[]l,char[]r){for(int x=0,y=0;x<s.length&&y<l.length;x++)if(s[x]==l[y])s[x]=r[y++];}
```
**Detailed** [*try here*](http://ideone.com/rOGcgA)
```
// String, Lookup, Replacement
void L(char[]s, char[]l, char[]r)
{
    for(int x=0, y=0; x < s.length && y < l.length; x++)
        if(s[x] == l[y])
            s[x] = r[y++];
}
```
[Answer]
# Julia, ~~93~~ ~~90~~ 86 bytes
```
f(s,p,r)=(try s=join([match(Regex(join("^$p\$","(.*?)")),s).captures';[r...""]])end;s)
```
Having to test separately if the match was successful kinda destroys the score. A substitution would require casting to `Base.SubstitutionString`, which probably isn't worth it...
### Test run
```
julia> f(s,p,r)=(try s=join([match(Regex(join("^$p\$","(.*?)")),s).captures';[r...""]])end;s)
f (generic function with 1 method)
julia> f("aacbcbabcccaabcbabcaabbbbca","abaaaccbac","1223334444")
"aacbcbabcccaabcbabcaabbbbca"
julia> f("aacbcbabcccaabcbabcaabbbbcac","abaaaccbac","1223334444")
"1ac2cb2bccc33b3bab4aa4bbbc44"
```
[Answer]
# Julia, ~~62~~ ~~59~~ 58 bytes
```
f(s,p,r)=(try s[[i=findnext(s,c,i+1)for c=p]'],i=r,0end;s)
```
I/O is in form of character arrays.
### Verification
```
julia> f(s,p,r)=(try s[[i=findnext(s,c,i+1)for c=p]'],i=r,0end;s)
f (generic function with 2 methods)
julia> F(s,p,r)=join(f([s...],[p...],[r...])) # string/char array conversion
F (generic function with 1 method)
julia> F("aacbcbabcccaabcbabcaabbbbca","abaaaccbac","1223334444")
"aacbcbabcccaabcbabcaabbbbca"
julia> F("aacbcbabcccaabcbabcaabbbbcac","abaaaccbac","1223334444")
"1ac2cb2bccc33b3bab4aa4bbbc44"
```
[Answer]
# PHP, ~~130~~ 109 bytes
I´d still like it shorter; could save 3 bytes (`""<`) if `B` was guaranteed to not contain `0`.
```
for($s=($a=$argv)[1];""<$c=$a[2][$i++];)if($p=strpos(_.$s,$c,$p+1))$s[$p-1]=$a[3][$k++];echo$k<$i-1?$a[1]:$s;
```
takes arguments from command line. Run with `-r`.
Replaces the characters when it finds them;  
prints copy if all characters have been replaced; original else.
[Answer]
# Ruby, ~~70~~ ~~64~~ ~~59~~ 58 bytes
Anonymous function. Walk through the string `a` to build a new string with letters replaced in accordance to the next character in `b` and `c`, then if all characters in `b` are exhausted at the end, return the newly constructed string, otherwise return the original string.
@histocrat helped save 6 bytes via `gsub`.
Saved 1 byte thanks to @Cyoce.
```
->a,b,c{i=0;s=a.gsub(/./){$&==b[i]?c[~-i+=1]:$&};b[i]?a:s}
```
[Try it online!](https://tio.run/nexus/ruby#S7ON@a9rl6iTpJNcnWlrYF1sm6iXXlyapKGvp69ZraJma5sUnRlrnxxdp5upbWsYa6WiVmsNFkq0Kq79X1BaUqyQppecmJOjkJ5aUqyXnJGfW6CDg/0/NSU5KREbwZWYlJySymVoZGxiCgA "Ruby – TIO Nexus")
[Answer]
# Perl, 80 + 1 = 81 bytes
Run with the `-p` flag
```
$a=join"(.*?)",split//,<>;$b.=$_." .\$".++$;."."for split//,<>;chop$b;s/$a/$b/ee
```
[Try it online!](https://tio.run/nexus/perl#@6@SaJuVn5mnpKGnZa@ppFNckJNZoq@vY2NnrZKkZ6sSr6ekoBejoqSnra1iraekp5SWX6SApCg5I79AJcm6WF8lUV8lST819f//xKTklNTUlOSkRDCLKzWFy9Dov27Bf11fUz0DQwMA "Perl – TIO Nexus")
The code procedurally generates a search and replace regex command, which it then executes in the last bit of code.
The string `ghost` in the first example gets turned into the string `g(.*?)h(.*?)o(.*?)s(.*?)t(.*?)`, which means a `g` followed by 0 or more characters, followed by an `h` followed by 0 or more characters, followed by etc. The `*?` quantifier means that the search should be non-greedy and "gobble" as few characters as possible, instead of the default of matching as much as possible.
The string `12345` then gets turned into `1 .$1.2 .$2.3 .$3.4 .$4.5 .$5`, which gets evaluated after the regex is performed. Each of `$1,$2,$3,$4,$5` is actually a backreference to a capture group (in parentheses) from the first string.
[Answer]
## Clojure, 113 bytes
```
#(apply str((reduce(fn[[b c r]a](if(=(first b)a)[(rest b)(rest c)(conj r(first c))][b c(conj r a)]))[%2%3[]]%)2))
```
A basic `reduce`, not too happy about all those long `first`, `rest` and `conj` function calls. Hoping to see a better approach.
]