# 最长回文子串

<p>给你一个字符串 <code>s</code>，找到 <code>s</code> 中最长的回文子串。</p><p> </p><p><strong>示例 1：</strong></p><pre><strong>输入：</strong>s = "babad"<strong><br />输出：</strong>"bab"<strong><br />解释：</strong>"aba" 同样是符合题意的答案。</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>s = "cbbd"<strong><br />输出：</strong>"bb"</pre><p><strong>示例 3：</strong></p><pre><strong>输入：</strong>s = "a"<strong><br />输出：</strong>"a"</pre><p><strong>示例 4：</strong></p><pre><strong>输入：</strong>s = "ac"<strong><br />输出：</strong>"a"</pre><p> </p><p><strong>提示：</strong></p><ul>	<li><code>1 <= s.length <= 1000</code></li>	<li><code>s</code> 仅由数字和英文字母（大写和/或小写）组成</li></ul>

以下程序实现了这一功能，请你填补空白处内容：

```cpp
class Solution
{
public:
	string longestPalindrome(string s)
	{
		int ti = 0, maxlen = 0, i, t;
		for (i = 0; s[i]; i++)
		{
			t = 1;
			while (t <= i && s[i + t])
			{
				if (s[i + t] == s[i - t])
					t++;
				else
					break;
			}
			t--;
			if (2 * t + 1 > maxlen)
			{
				ti = i - t;
				maxlen = 2 * t + 1;
			}
		}
		for (i = 0; s[i]; i++)
		{
			t = 1;
			while (t <= i + 1 && s[i + t])
			{
				if (s[i - t + 1] == s[i + t])
					t++;
				else
					break;
			}
			t--;
			________________;
		}
		s[ti + maxlen] = 0;
		return s.c_str() + ti;
	}
};
```

## template

```cpp
class Solution
{
public:
	string longestPalindrome(string s)
	{
		int ti = 0, maxlen = 0, i, t;
		for (i = 0; s[i]; i++)
		{
			t = 1;
			while (t <= i && s[i + t])
			{
				if (s[i + t] == s[i - t])
					t++;
				else
					break;
			}
			t--;
			if (2 * t + 1 > maxlen)
			{
				ti = i - t;
				maxlen = 2 * t + 1;
			}
		}
		for (i = 0; s[i]; i++)
		{
			t = 1;
			while (t <= i + 1 && s[i + t])
			{
				if (s[i - t + 1] == s[i + t])
					t++;
				else
					break;
			}
			t--;
			if (2 * t > maxlen)
			{
				ti = i - t + 1;
				maxlen = 2 * t;
			}
		}
		s[ti + maxlen] = 0;
		return s.c_str() + ti;
	}
};
```

## 答案

```cpp
if (2 * t > maxlen)
{
	ti = i - t + 1;
	maxlen = 2 * t;
}
```

## 选项

### A

```cpp
if (t > maxlen)
{
	ti = i - t + 1;
	maxlen = 2 * t;
}
```

### B

```cpp
if (2 * t > maxlen)
{
	ti = i + t + 1;
	maxlen = 2 * t;
}
```

### C

```cpp
if (t > maxlen)
{
	ti = i + t + 1;
	maxlen = 2 * t;
}
```