StringMatching.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*******************************************************************************
  2. The content of this file includes portions of the proprietary AUDIOKINETIC Wwise
  3. Technology released in source code form as part of the game integration package.
  4. The content of this file may not be used without valid licenses to the
  5. AUDIOKINETIC Wwise Technology.
  6. Note that the use of the game engine is subject to the Unreal(R) Engine End User
  7. License Agreement at https://www.unrealengine.com/en-US/eula/unreal
  8. License Usage
  9. Licensees holding valid licenses to the AUDIOKINETIC Wwise Technology may use
  10. this file in accordance with the end user license agreement provided with the
  11. software or, alternatively, in accordance with the terms contained
  12. in a written agreement between you and Audiokinetic Inc.
  13. Copyright (c) 2023 Audiokinetic Inc.
  14. *******************************************************************************/
  15. /*=============================================================================
  16. StringMatching.h:
  17. =============================================================================*/
  18. #pragma once
  19. #include "CoreMinimal.h"
  20. #include "Array2D.h"
  21. // Longest Common Substring
  22. namespace LCS
  23. {
  24. struct StringComparer
  25. {
  26. StringComparer(FString in_a, FString in_b)
  27. {
  28. a = in_a;
  29. b = in_b;
  30. }
  31. void lcs()
  32. {
  33. int32 m = a.Len();
  34. int32 n = b.Len();
  35. Array2D<int32> suffixLengths(m + 1, n + 1, 0);
  36. int32 result = 0;
  37. int32 aIdx = 0;
  38. int32 bIdx = 0;
  39. for (int32 i = 0; i <= m; i++)
  40. {
  41. for (int32 j = 0; j <= n; j++)
  42. {
  43. if (i == 0 || j == 0)
  44. suffixLengths(i, j) = 0;
  45. else if (a[i - 1] == b[j - 1])
  46. {
  47. suffixLengths(i, j) = suffixLengths(i - 1, j - 1) + 1;
  48. if (suffixLengths(i, j) > result)
  49. {
  50. result = suffixLengths(i, j);
  51. aIdx = i;
  52. bIdx = j;
  53. }
  54. }
  55. else
  56. suffixLengths(i, j) = 0;
  57. }
  58. }
  59. if (result > 2)
  60. {
  61. a.RemoveAt(aIdx - result, result);
  62. b.RemoveAt(bIdx - result, result);
  63. lcs();
  64. }
  65. }
  66. FString a;
  67. FString b;
  68. };
  69. // Obtain the length of the longest common sub-string between two strings
  70. // https://en.wikipedia.org/wiki/Longest_common_substring_problem
  71. float GetLCSScore(const FString& a, const FString& b);
  72. };