using System;
using System.Collections.Generic;
class Program
{
static bool IsInDict(string word, List<string> dict) {
foreach (var entry in dict) {
if (entry == word)
return true;
}
return false;
}
static void WordBreak(string str, string result, List<string> dict) {
int strsize = str.Length;
for (int i = 1; i <= strsize; i++) {
string subStr = str.Substring(0, i);
if (IsInDict(subStr, dict)) {
if (i == strsize) {
Console.WriteLine(result + subStr);
return;
}
WordBreak(str.Substring(i), result + subStr + " ", dict);
}
}
}
static void Main()
{
string str = "butterflyplaybasketballwithbags";
List<string> dict = new List<string> {
"butterfly", "basketball", "bagpiper", "and", "play",
"with", "butter", "fly", "basket", "ball", "bags"
};
WordBreak(str, "", dict);
}
}
/*
run:
butter fly play basket ball with bags
butter fly play basketball with bags
butterfly play basket ball with bags
butterfly play basketball with bags
*/