題目 2—全在其中(考核+1)
a)撰寫函式 bool isExist(string s1, string s2)
判斷 s1 是否 s2 的”子列”。
b)提示:用 string 的 find 函式
c) 處理輸入輸出見右圖參考代碼。 中 isExist 是需要撰寫的函式。
d) 測驗輸入輸出 首先在本機測驗,可用復制粘貼的 方法測驗。然后再測驗如下輸入:
aaab a ,應該輸出啥?
主題
bool isExist(string s1, string s2);
int main()
{
string str1, str2;
while (cin >> str1 >> str2)
{
if (isExist(str1, str2))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
uj5u.com熱心網友回復:
bool isExist(string s1, string s2){
int jug;
jug=s1.find(s2);
if (jug != -1)return true;
else return false;
}
直接運用find函式對s1中的s2進行查找 比較偷懶的方法
uj5u.com熱心網友回復:
另外一個思路是bool isExist(string s1, string s2) {
int m = s1.length();
int n = s2.length();
if (n == 0) return true;
for (int i = 0;i < m;i++) {
int j = 0;
int t = i;
while (s1[t] == s2[j] && j < n) {
j++;
if (s1[t] != s2[j] && j < n) {
break;
}
}
if (j == n) {
return true;
}
}
return false;
}
uj5u.com熱心網友回復:
對上式的注釋
int m = s1.length();//計算字串的長度
int n = s2.length();
while (s1[t] == s2[j] && j < n) {//比較各字符是否相等來確定s1中是否有s2
if (j == n) { //比較到第n個(s2[n-1])
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/283637.html
標籤:新手樂園