日期:2025年11月18日
ZeroJudge 題目連結:i510. 尋找子字串
解題想法
這題用字串的 find 功能很好寫。
Python 程式碼
使用時間約為 17 ms,記憶體約為 6.1 MB,通過測試。
import sys
for line in sys.stdin:
n, m = map(int, line.split())
s, t = sys.stdin.readline().split()
idx = s.find(t)
if idx == -1: print("No")
else: print(f"Yes\npos: {idx:d}")
C++ 程式碼
使用時間約為 0.1 s,記憶體約為 4.2 MB,通過測試。
#include <iostream>
#include <string>
using namespace std;
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int n, m;
while(cin >> n >> m) {
string s, t; cin >> s >> t;
auto idx = s.find(t);
if (idx == string::npos) cout << "No\n";
else cout << "Yes\n" << "pos: " << idx << "\n";
}
return 0;
}
沒有留言:
張貼留言