習題3-9 子序列(All in All, UVa 10340)
阿新 • • 發佈:2019-01-27
輸入兩個字串s和t,判斷是否可以從t中刪除0個或多個字元(其他字元順序不變),
得到字串s。例如,abcde可以得到bce,但無法得到dc。
提示:t是較長的那個序列,s是要得到的那個序列,思路就是先把“指標”指向s中的第一個字元,依次遍歷t序列,找到s中的第一個字元後,“指標”後移一個,繼續遍歷t,如果指標移動到s的末尾,說明可以,否則就不行。#include <stdio.h> #include <string.h> #define maxn 100 int main() { char s[maxn],t[maxn]; scanf("%s",s); scanf("%s",t); int i=0; for(int j=0;j<strlen(t);j++) { if(t[j]==s[i]) i++; if(i==strlen(s)) break; } if(i==strlen(s)) printf("Yes\n"); else printf("No\n"); }