/
DeC2018
/
0028
Обзор
Документация
Войти
/
DeC2018
/
0028
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
main.cpp
36 строк
994 B
Den
create main.cpp
03 янв 2025, 02:52
03 янв 2025, 02:52
370e9ad
Код
Авторство
О чём код?
#include <iostream> #include <string> class Solution { public: int strStr(std::string haystack, std::string needle) { const int m = haystack.length(); const int n = needle.length(); for (int i = 0; i <= m - n; i++) { if (haystack.substr(i, n) == needle) { return i; } } return -1; } }; int main() { Solution solution; // Test case 1 std::string haystack1 = "sadbutsad"; std::string needle1 = "sad"; std::cout << "Input: haystack = \"" << haystack1 << "\", needle = \"" << needle1 << "\"\n"; std::cout << "Output: " << solution.strStr(haystack1, needle1) << "\n"; // Output: 0 // Test case 2 std::string haystack2 = "leetcode"; std::string needle2 = "leeto"; std::cout << "Input: haystack = \"" << haystack2 << "\", needle = \"" << needle2 << "\"\n"; std::cout << "Output: " << solution.strStr(haystack2, needle2) << "\n"; // Output: -1 return 0; }