[英]Odd/curious behaviour in a recursive function
我正在嘗試對一個函數進行編程,以檢查字符串是否是前一個字符串上的一組操作的結果。 具體來說,字符串“ b”是字符串“ a”的變換,如果它遵循相同的字符順序,僅具有相同的字符,但可能會將它們相乘。 例如: "aabba"
是"aba"
的轉換,而不是"abaa"
,因為它的末尾有雙倍的'a'
。
代碼如下:
public static boolean isTrans(String s, String t)
{
if(s.equals(t)){return true;} //obviously the latter is transformation
else if(s.length()==0||t.length()==0){return false;} //either is empty, can't be transformation
else if(s.charAt(0)!=t.charAt(0)){return false;} //obviously not transformation
else {return (s.length()==(isTrans(s,0,t,1)));} //recursive method below, note s.charAt(0)==t.charAt(0).
}
private static int isTrans(String s, int i, String t, int j)
{
if(i < s.length() && j < t.length()) //only as long as the indexes are within right bound
{
if(s.charAt(i) == t.charAt(j)) //character at 'transformed' string is the character at original string
{
if((j+1)<t.length()) //only as long as there are more characters
{
j++;
isTrans(s,i,t,j); //check next character at transformed string
}
}
else if((i+1)<s.length()) //ensures there is a next character at string s
{
if(s.charAt(i+1) == t.charAt(j)) //character is the next chracter at original string
{
i++;
if((j+1)<t.length()) //only as long as there are more characters
{
j++;
isTrans(s,i,t,j); //checks next characters at strings
}
}
else{i=-1;} //not transformation
}
else{i=-1;} //not transformation
}
return (i+1);
}
該程序無法正常運行。 現在,這很奇怪:通過調試器運行它,可以按預期執行所有操作,但是,當它到達“ return (i+1)
”命令時,而不是實際返回它,而是開始對某些對象執行遞歸調用原因,同時減小i
的值,直到它達到0,然后才返回它,從而導致假陰性。 更具體地說,它沿棧上升並“執行” isTrans(s,i,t,j)
的遞歸調用。
我想知道為什么會這樣做,這不僅是解決此類問題的方法。 它甚至沒有通過iff進入,而是立即進入遞歸調用,將i
的值一直減小到0,然后才返回。
感謝任何評論!
編輯:根據調試器,弄清楚它的確切作用。 如果我嘗試查看"aabba"
是否是"aba"
的轉換(在上面的定義下),則程序將達到i
- 2
的所需值。 但是,它隨后到達命令return (i+1)
,並突然返回給定代碼中的第17行,然后返回代碼中的下一個遞歸調用,並返回到該代碼-同時將i
的值減小為0
。 只有這樣,它才能在函數外部執行命令。
編輯2:經過一些調整和使用代碼后,似乎沒有連接到return語句 ,最后函數“向后跳轉”,跳過了“ if”,然后遞歸調用-不執行它們,卻將i
降低為0
,然后才繼續。 它與調用isTrans(s, 0 ,t,1)有關嗎?
您的退出條件是return i+1
。 即使您的邏輯做到了,這也永遠不會起作用,因為您返回的結果總是比String的長度大一。
這是您的代碼中可能發生的情況:
為此,您可能需要執行以下操作:
public class Main {
public static void main(String args[])
{
String s = "aba";
String t = "abz";
System.out.println(isTrans(0, 0, s, t));
}
public static boolean isTrans(int si, int ti,String s ,String t)
{
// we have run out of source characters, or the source character has passed the transformation character. This is not a transformation.
if (si > ti || si == s.length())
{
return false;
}
// we are at the end of the transformation string. This might be a transformation.
if(ti == t.length())
{
return si == s.length()-1; // If we're at the end of our source string, otherwise it isn't.
}
// if the current positions contain identical characters...
if (checkChar(si, ti, s, t))
{
// advance the transformation index.
ti++;
}
else
{
// otherwise, advance the source index.
si++;
}
// recursion.
return isTrans(si, ti, s, t);
}
private static boolean checkChar(int si, int ti, String s, String t)
{
return (s.charAt(si) == t.charAt(ti));
}
}
希望對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.