A105166.朴实无华的瑟提
2026-04-19 13:50:20
发布于:河南
10阅读
0回复
0点赞
这道题用到了双指针的思想,根据题目意思,我们需要建立一个左指针指向字符串s[0],右指针指向字符串s[n - 1],然后用while 不断向中间判断,判断条件是当左指针不大于右指针且s[i] != s[j],最后输出两指针最后相聚的距离即可,看代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--) {
int n;
string s;
cin >> n >> s;
int i = 0, j = n - 1;
while (i < j && s[i] != s[j]) {
i++;
j--;
}
cout << j - i + 1 << "\n";
}
return 0;
}
这里空空如也


有帮助,赞一个