我很抱歉我的英語。 我需要在所有變化中劃分字符串的函數,順序和長度保持不變。如何在PHP中的所有變體中劃分字符串
輸入 'ABC'
輸出 'ABC/A,BC/AB,C/A,B,C'
或
輸入 'RRD'
輸出「RRD/R,RD/RR,d/R,R,d」
謝謝
我很抱歉我的英語。 我需要在所有變化中劃分字符串的函數,順序和長度保持不變。如何在PHP中的所有變體中劃分字符串
輸入 'ABC'
輸出 'ABC/A,BC/AB,C/A,B,C'
或
輸入 'RRD'
輸出「RRD/R,RD/RR,d/R,R,d」
謝謝
檢查這個 '美麗' 的代碼,漂亮的腦TRAI對我來說:)
它絕對需要一些優化,但作品完美。
注意:array_reverse()和strrev()可以被移除,但是這樣順序看起來更好。
function TheFunction($s) {
for($i=strlen($s)-1;$i>0;$i--) $h .= '1';
$z = str_replace('1','0',$h);
for($i=bindec($h);$i>=0;$i--) $array[] = strrev(substr_replace($z, decbin($i), strlen($z)-strlen(decbin($i))));
foreach($array as $value){
$value = str_replace(array('0','1'),array(' ',','),$value);
$string = '';
for($i=0;$i<strlen($s)-1;$i++) $string .= $s[$i].$value[$i];
$string .= $s[strlen($s)-1];
$results[] = str_replace(' ','',$string);
}
return array_reverse($results);
}
例
print_r(TheFunction('Anne'));
返回
Array
(
[0] => Anne
[1] => A,nne
[2] => An,ne
[3] => A,n,ne
[4] => Ann,e
[5] => A,nn,e
[6] => An,n,e
[7] => A,n,n,e
)
另一個實施例
print_r(TheFunction('Stack'));
返回:
Array
(
[0] => Stack
[1] => S,tack
[2] => St,ack
[3] => S,t,ack
[4] => Sta,ck
[5] => S,ta,ck
[6] => St,a,ck
[7] => S,t,a,ck
[8] => Stac,k
[9] => S,tac,k
[10] => St,ac,k
[11] => S,t,ac,k
[12] => Sta,c,k
[13] => S,ta,c,k
[14] => St,a,c,k
[15] => S,t,a,c,k
)
我做了類似安妮的事情,除了我使用了Adrian Akison here的變體類。
正如在Anne的解決方案中,我找到了1和0的所有變體(Variations.cs),長度比原始字符串小1。這是爲了抵消逗號的考慮。請注意,稍後,我會將1作爲逗號替換,將0作爲空格替換。
「位」 字符串111 = 「,,,」 或101 = 「」 等
所以,得到使用某些字符一定大小的所有變體,在該殼體1和0:
private HashSet<string> fetchBinaryVariations(int size)
{
HashSet<string> returnVal = new HashSet<string>();
String variationResultItem = string.Empty;
string[] oneZero = { "1", "0" };
/* Generate all variations of 1's and 0's given size using Adrian Akison's handy dandy variations class */
Variations<string> variationsList = new Variations<string>(oneZero.ToList<string>(), size, GenerateOption.WithRepetition);
Console.WriteLine("Total Variations: {0}", variationsList.Count());
foreach (List<string> variationItem in variationsList)
{
variationResultItem = String.Join("", variationItem);
returnVal.Add(variationResultItem);
// Console.WriteLine("Variation: {0}", variationResultItem);
}
return returnVal;
}
接下來,我把這些「位」的字符串,它們與我原來的順序轉換成逗號和空格和合並。在我的情況下,我有另一個步驟,我使用enum(未顯示)將數字解碼爲字母:
Ex。 「位」字符串= 101 =將',,'的分隔符添加到原始序列1234 ='1,2 3,4'
Ex。 「位」 串= 111 =的deliminators ',,,' 加到1234 =原始序列 '1,2,3,4'
private Dictionary<string, string> processDeliminatorsWithInputSequence(string sequence, HashSet<string> binaryVariations)
{
Dictionary<string, string> returnVal = new Dictionary<string, string>();
string message = string.Empty, variationWithDelim = string.Empty, finalString = string.Empty;
StringBuilder characterContainer = null;
int satisfiedCnt = 0, unsatisfiedCnt = 0;
foreach (string variation in binaryVariations)
{
variationWithDelim = variation.Replace('0', ' ').Replace('1', ','); // 0's are spaces and 1's are commas
characterContainer = new StringBuilder();
for (int i = 0; i < sequence.Length - 1; i++)
{
characterContainer.Append(sequence[i]); // Original Input
characterContainer.Append(variationWithDelim[i]); // Append with space or comma
}
characterContainer.Append(sequence[sequence.Length - 1]); // Need to append last character from original input - offset again
characterContainer.Replace(" ", ""); // Clean up empty spaces in final string
finalString = decodeToAlphabet(characterContainer); // converat numerals to their alpha equivelant
if (finalString != null)
returnVal.Add(characterContainer.ToString(), finalString); // Add original encoding and decoded strings to hastable
else
unsatisfiedCnt++;
satisfiedCnt = returnVal.Count();
}
message = String.Format("Input Sequence: {0}\r\nInput Binary Variations: {1}\r\n", sequence, binaryVariations.Count());
message += String.Format("Valid Alphabet Sequence Variations: {0}\r\nInvalid Alphabet Sequence Variations: {1}", satisfiedCnt, unsatisfiedCnt);
result.Messsage = message;
Console.WriteLine(message);
return returnVal;
}
是,超強!謝謝 – juro 2011-04-10 21:42:17