Problem: 2050E - Three Strings
Language: Python 3

Previous submission (verdict: Runtime error on test 1):
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e54c80f1-8818-4816-ba60-95d17a7b366e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "0\n",
      "3\n",
      "0\n",
      "4\n",
      "8\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "def min_changes_needed(t, test_cases):\n",
    "    results = []\n",
    "    \n",
    "    for a, b, c in test_cases:\n",
    "        n, m = len(a), len(b)\n",
    "        dp = [[0] * (m + 1) for _ in range(n + 1)]\n",
    "        \n",
    "        for i in range(n + 1):\n",
    "            for j in range(m + 1):\n",
    "                if i + j == 0:\n",
    "                    continue\n",
    "                c_index = i + j - 1\n",
    "                if i > 0 and c_index < len(c) and a[i - 1] == c[c_index]:\n",
    "                    dp[i][j] = max(dp[i][j], dp[i - 1][j] + 1)\n",
    "                if j > 0 and c_index < len(c) and b[j - 1] == c[c_index]:\n",
    "                    dp[i][j] = max(dp[i][j], dp[i][j - 1] + 1)\n",
    "        \n",
    "        max_matched = dp[n][m]\n",
    "        changes_needed = (n + m) - max_matched\n",
    "        results.append(changes_needed)\n",
    "    \n",
    "    return results\n",
    "\n",
    "# Example Input\n",
    "t = 7 \n",
    "test_cases = [\n",
    "    (\"a\", \"b\", \"cb\"),\n",
    "    (\"ab\", \"cd\", \"acbd\"),\n",
    "    (\"ab\", \"ba\", \"aabb\"),\n",
    "    (\"xxx\", \"yyy\", \"xyxyxy\"),\n",
    "    (\"a\", \"bcd\", \"decf\"),\n",
    "    (\"codes\", \"horse\", \"codeforces\"),\n",
    "    (\"egg\", \"annie\", \"egaegaeg\")\n",
    "]\n",
    "\n",
    "# Function call\n",
    "outputs = min_changes_needed(t, test_cases)\n",
    "for output in outputs:\n",
    "    print(output)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9d7d4f76-4765-403f-9d8d-65050c7f584e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "0\n",
      "3\n",
      "0\n",
      "4\n",
      "8\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "def min_changes_needed(t, test_cases):\n",
    "    results = []\n",
    "    \n",
    "    for a, b, c in test_cases:\n",
    "        n, m = len(a), len(b)\n",
    "        # Initialize DP table with zeros\n",
    "        dp = [[0] * (m + 1) for _ in range(n + 1)]\n",
    "        \n",
    "        for i in range(n + 1):\n",
    "            for j in range(m + 1):\n",
    "                if i + j == 0:\n",
    "                    continue\n",
    "                c_index = i + j - 1\n",
    "                if i > 0 and c_index < len(c) and a[i - 1] == c[c_index]:\n",
    "                    dp[i][j] = max(dp[i][j], dp[i - 1][j] + 1)\n",
    "                if j > 0 and c_index < len(c) and b[j - 1] == c[c_index]:\n",
    "                    dp[i][j] = max(dp[i][j], dp[i][j - 1] + 1)\n",
    "        \n",
    "        max_matched = dp[n][m]\n",
    "        changes_needed = (n + m) - max_matched\n",
    "        results.append(changes_needed)\n",
    "    \n",
    "    return results\n",
    "\n",
    "# Example Input\n",
    "t = 7 \n",
    "test_cases = [\n",
    "    (\"a\", \"b\", \"cb\"),\n",
    "    (\"ab\", \"cd\", \"acbd\"),\n",
    "    (\"ab\", \"ba\", \"aabb\"),\n",
    "    (\"xxx\", \"yyy\", \"xyxyxy\"),\n",
    "    (\"a\", \"bcd\", \"decf\"),\n",
    "    (\"codes\", \"horse\", \"codeforces\"),\n",
    "    (\"egg\", \"annie\", \"egaegaeg\")\n",
    "]\n",
    "\n",
    "# Function call\n",
    "outputs = min_changes_needed(t, test_cases)\n",
    "for output in outputs:\n",
    "    print(output)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "38f720fb-90f0-49f5-a0c7-246515df6999",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "0\n",
      "3\n",
      "0\n",
      "4\n",
      "8\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "def min_changes_needed(t, test_cases):\n",
    "    results = []\n",
    "    \n",
    "    for a, b, c in test_cases:\n",
    "        n, m = len(a), len(b)\n",
    "        # Initialize DP table\n",
    "        dp = [[0] * (m + 1) for _ in range(n + 1)]\n",
    "        \n",
    "        # Fill DP table\n",
    "        for i in range(n + 1):\n",
    "            for j in range(m + 1):\n",
    "                # If there are no characters taken from both a and b\n",
    "                if i == 0 and j == 0:\n",
    "                    continue\n",
    "                \n",
    "                c_index = i + j - 1  # c_index refers to the current character in c we're matching with\n",
    "                \n",
    "                # Check if we can match from a\n",
    "                if i > 0 and c_index < len(c) and a[i - 1] == c[c_index]:\n",
    "                    dp[i][j] = max(dp[i][j], dp[i - 1][j] + 1)\n",
    "                \n",
    "                # Check if we can match from b\n",
    "                if j > 0 and c_index < len(c) and b[j - 1] == c[c_index]:\n",
    "                    dp[i][j] = max(dp[i][j], dp[i][j - 1] + 1)\n",
    "        \n",
    "        # Maximum number of characters from c matched correctly\n",
    "        max_matched = dp[n][m]\n",
    "        # Calculating the changes needed\n",
    "        changes_needed = (n + m) - max_matched  # Total length - matched\n",
    "        results.append(changes_needed)\n",
    "    \n",
    "    return results\n",
    "\n",
    "# Example Input\n",
    "t = 7 \n",
    "test_cases = [\n",
    "    (\"a\", \"b\", \"cb\"),\n",
    "    (\"ab\", \"cd\", \"acbd\"),\n",
    "    (\"ab\", \"ba\", \"aabb\"),\n",
    "    (\"xxx\", \"yyy\", \"xyxyxy\"),\n",
    "    (\"a\", \"bcd\", \"decf\"),\n",
    "    (\"codes\", \"horse\", \"codeforces\"),\n",
    "    (\"egg\", \"annie\", \"egaegaeg\")\n",
    "]\n",
    "\n",
    "# Function call\n",
    "outputs = min_changes_needed(t, test_cases)\n",
    "for output in outputs:\n",
    "    print(output)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ab5d85e2-a2b0-4b95-a810-f5b56d3d8cf3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}


Submission (verdict: Wrong answer on test 1):
def min_changes_needed(t, test_cases):
    results = []
    
    for a, b, c in test_cases:
        n, m = len(a), len(b)
        dp = [[0] * (m + 1) for _ in range(n + 1)]
        
        for i in range(n + 1):
            for j in range(m + 1):
                c_index = i + j - 1
                if i == 0 and j == 0:
                    continue
                if c_index >= len(c):
                    continue
                if i > 0 and a[i - 1] == c[c_index]:
                    dp[i][j] = max(dp[i][j], dp[i - 1][j] + 1)
                if j > 0 and b[j - 1] == c[c_index]:
                    dp[i][j] = max(dp[i][j], dp[i][j - 1] + 1)
        max_matched = dp[n][m]
        changes_needed = (n + m) - max_matched
        results.append(changes_needed)
    return results
t = 7 
test_cases = [
    ("a", "b", "cb"),
    ("ab", "cd", "acbd"),
    ("ab", "ba", "aabb"),
    ("xxx", "yyy", "xyxyxy"),
    ("a", "bcd", "decf"),
    ("codes", "horse", "codeforces"),
    ("egg", "annie", "egaegaeg")
]
outputs = min_changes_needed(t, test_cases)
for output in outputs:
    print(output)