Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by aditya_tirodkar · Dec 06, 2012 at 07:46 PM · stringloopexceptionsyntaxrecursion

Longest Common Subsequence Recursive Function Error.

Hi, I have written the following function to calculate the Longest Common Subsequence between two strings:

 function lcs(a, b): String {
   var aSub = a.Substring(0, a.Length-1);
   var bSub = b.Substring(0, b.Length-1);
 if (a.Length == 0 || b.Length == 0) {
     return "";
   } else if (a[a.Length-1] == b[b.Length-1]) {
     return lcs(aSub, bSub) + a[a.Length-1];
   } else {
     var x = lcs(a, bSub);
     var y = lcs(aSub, b);
     return (x.Length > y.Length) ? x : y;
   }
 }

However, when I run it, Unity gives me a negative number exception on the second line. On modifying it to say a.Length, Unity crashed. Can someone tell me if this is a problem with Unity handling recursive functions or is my code flawed somewhere.

I essentially have a="start" and b="startblue1stepbgreen2stepgred1stepryellow2stepy". Thus, the function should return "start".

THanks!

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Screenhog · Dec 06, 2012 at 09:41 PM 0
Share

I haven't heard of "Longest Common Subsequence". What does that mean?

avatar image Owen-Reynolds · Dec 06, 2012 at 10:54 PM 1
Share

LCS is something all 2nd year ComSci students know, and no one else :-)

There's a well-known much more efficient non-recursive method involving filling in a 2D array (it unwraps the recursion.) Runs in O(n^2). The recursive version is more like O(2^n).

I'd guess if you tossed in a global `count`, inc'ed it and kicked out at 2,000, (for fun print A and B,) you might just be blowing out the stack.

avatar image aditya_tirodkar · Dec 07, 2012 at 07:21 AM 0
Share

Hmmm... but then, is there any way I can use this without screwing up the stack? The code works fine if used in an editor.... It's just Unity that's giving me problems...

avatar image Owen-Reynolds · Dec 07, 2012 at 03:12 PM 0
Share

What I mean is, I'm thinking this approach may run super horribly slow on longer strings, and you may have to rewrite it the "good" way. Try it on two long string w/o much in common.

avatar image Bunny83 · Dec 07, 2012 at 07:27 PM 0
Share

The problem is actually well known but maybe not under this name ;) A simple code / file-diff program have to solve this to find the differences between two files. LCS

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by whydoidoit · Dec 07, 2012 at 09:29 AM

I would have thought that your test on line 4 ought to be of aSub and bSub not of a and b

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Dec 07, 2012 at 09:30 AM 1
Share

Or that test should be the first thing that you do before calculating aSub and bSub...

avatar image Bunny83 · Dec 07, 2012 at 07:17 PM 0
Share

Yes and that's the problem ;) When one of the strings passed to the function has a lenght of 0 you will execute SubString(0,-1) which will cause an ArgumentOutOfRangeException according to the specs of SubString,

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Loop through string? (foreach new line) 3 Answers

Stop working after for loop appending string 1 Answer

How to make AudioSource play defined clips using String? 1 Answer

ExecutionEngineException: SIGILL 0 Answers

Getting an error with my for/in loop! 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges