#Java 7, 256 bytes
Java 7, 256 bytes
import java.util.*;String c(String...a){Map s=new HashMap();int j,i=1,l=a[0].length();for(;i<a.length;i++)if((j=a[0].indexOf(a[i]))>-1)s.put(j,s.get(j)!=null?s.get(j)+" "+a[i]:a[i]);a[0]="";for(j=0;j<l;j++)a[0]+=s.get(j)!=null?s.get(j)+" ":"";return a[0];}
It should definitely be possible to golf this more by using a different approach, but this will do for now..
Ungolfed & test code:
import java.util.*;
class M{
static String c(String... a){
Map s = new HashMap();
int j,
i = 1,
l = a[0].length();
for(; i < a.length; i++){
if((j = a[0].indexOf(a[i])) > -1){
s.put(j, s.get(j) != null
? s.get(j) + " " + a[i]
: a[i]);
}
}
a[0] = "";
for(j = 0; j < l; j++){
a[0] += s.get(j) != null
? s.get(j) + " "
: "";
}
return a[0];
}
public static void main(String[] a){
System.out.println(c("dogcatfrog", "cat", "frog", "dog"));
System.out.println(c("xxcatfixsxhingonxgrapexxxfishingcxat", "cat", "grape", "catfish", "fishing"));
System.out.println(
c("dababbabadbaccbcbaaacdacdbdd ", "aa", "bb", "cc", "dd", "ba", "ba", "ba", "ab", "ac", "da", "db", "dc"));
System.out.println(c("flea", "antelope"));
}
}
Output:
dog cat frog
cat grape fishing
da ab ba ba ba bb db ac cc aa dd
#Java 7, 256 bytes
import java.util.*;String c(String...a){Map s=new HashMap();int j,i=1,l=a[0].length();for(;i<a.length;i++)if((j=a[0].indexOf(a[i]))>-1)s.put(j,s.get(j)!=null?s.get(j)+" "+a[i]:a[i]);a[0]="";for(j=0;j<l;j++)a[0]+=s.get(j)!=null?s.get(j)+" ":"";return a[0];}
It should definitely be possible to golf this more by using a different approach, but this will do for now..
Ungolfed & test code:
import java.util.*;
class M{
static String c(String... a){
Map s = new HashMap();
int j,
i = 1,
l = a[0].length();
for(; i < a.length; i++){
if((j = a[0].indexOf(a[i])) > -1){
s.put(j, s.get(j) != null
? s.get(j) + " " + a[i]
: a[i]);
}
}
a[0] = "";
for(j = 0; j < l; j++){
a[0] += s.get(j) != null
? s.get(j) + " "
: "";
}
return a[0];
}
public static void main(String[] a){
System.out.println(c("dogcatfrog", "cat", "frog", "dog"));
System.out.println(c("xxcatfixsxhingonxgrapexxxfishingcxat", "cat", "grape", "catfish", "fishing"));
System.out.println(
c("dababbabadbaccbcbaaacdacdbdd ", "aa", "bb", "cc", "dd", "ba", "ba", "ba", "ab", "ac", "da", "db", "dc"));
System.out.println(c("flea", "antelope"));
}
}
Output:
dog cat frog
cat grape fishing
da ab ba ba ba bb db ac cc aa dd
Java 7, 256 bytes
import java.util.*;String c(String...a){Map s=new HashMap();int j,i=1,l=a[0].length();for(;i<a.length;i++)if((j=a[0].indexOf(a[i]))>-1)s.put(j,s.get(j)!=null?s.get(j)+" "+a[i]:a[i]);a[0]="";for(j=0;j<l;j++)a[0]+=s.get(j)!=null?s.get(j)+" ":"";return a[0];}
It should definitely be possible to golf this more by using a different approach, but this will do for now..
Ungolfed & test code:
import java.util.*;
class M{
static String c(String... a){
Map s = new HashMap();
int j,
i = 1,
l = a[0].length();
for(; i < a.length; i++){
if((j = a[0].indexOf(a[i])) > -1){
s.put(j, s.get(j) != null
? s.get(j) + " " + a[i]
: a[i]);
}
}
a[0] = "";
for(j = 0; j < l; j++){
a[0] += s.get(j) != null
? s.get(j) + " "
: "";
}
return a[0];
}
public static void main(String[] a){
System.out.println(c("dogcatfrog", "cat", "frog", "dog"));
System.out.println(c("xxcatfixsxhingonxgrapexxxfishingcxat", "cat", "grape", "catfish", "fishing"));
System.out.println(
c("dababbabadbaccbcbaaacdacdbdd ", "aa", "bb", "cc", "dd", "ba", "ba", "ba", "ab", "ac", "da", "db", "dc"));
System.out.println(c("flea", "antelope"));
}
}
Output:
dog cat frog
cat grape fishing
da ab ba ba ba bb db ac cc aa dd
#Java 7, 256 bytes
import java.util.*;String c(String...a){Map s=new HashMap();int j,i=1,l=a[0].length();for(;i<a.length;i++)if((j=a[0].indexOf(a[i]))>-1)s.put(j,s.get(j)!=null?s.get(j)+" "+a[i]:a[i]);a[0]="";for(j=0;j<l;j++)a[0]+=s.get(j)!=null?s.get(j)+" ":"";return a[0];}
It should definitely be possible to golf this more by using a different approach, but this will do for now..
Ungolfed & test code:
import java.util.*;
class M{
static String c(String... a){
Map s = new HashMap();
int j,
i = 1,
l = a[0].length();
for(; i < a.length; i++){
if((j = a[0].indexOf(a[i])) > -1){
s.put(j, s.get(j) != null
? s.get(j) + " " + a[i]
: a[i]);
}
}
a[0] = "";
for(j = 0; j < l; j++){
a[0] += s.get(j) != null
? s.get(j) + " "
: "";
}
return a[0];
}
public static void main(String[] a){
System.out.println(c("dogcatfrog", "cat", "frog", "dog"));
System.out.println(c("xxcatfixsxhingonxgrapexxxfishingcxat", "cat", "grape", "catfish", "fishing"));
System.out.println(
c("dababbabadbaccbcbaaacdacdbdd ", "aa", "bb", "cc", "dd", "ba", "ba", "ba", "ab", "ac", "da", "db", "dc"));
System.out.println(c("flea", "antelope"));
}
}
Output:
dog cat frog
cat grape fishing
da ab ba ba ba bb db ac cc aa dd