1 条题解
-
0
思路分析
判断字符串 s 是否是 t 的子序列,可通过双指针法高效解决:
初始化指针:用两个指针 i(指向 s)和 j(指向 t),初始均为 0。 遍历字符:逐个比较 s[i] 和 t[j]: 若字符相等,i 和 j 均后移一位。 若字符不等,仅 j 后移一位,继续寻找匹配字符。 终止条件:当 i 遍历完 s(i == len(s)),说明 s 是 t 的子序列;若 j 遍历完 t 但 i 未遍历完 s,则不是。 #include #include
using namespace std;
int main(){ string strA, strB; while(cin>>strA>>strB){ int lenA = strA.length(); int lenB = strB.length(); int i = 0, j = 0; while(i < lenA && j < lenB){ if(strA[i] == strB[j]){ i++; } j++; } if(i == lenA) cout<<"Yes"<<endl; else cout<<"No"<<endl; } return 0; }
- 1
信息
- ID
- 937
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 5
- 标签
- 递交数
- 1
- 已通过
- 1
- 上传者