給定字串text和匹配字串pattern 輸出最短匹配序列的起止位置 不要求pattern連續
阿新 • • 發佈:2019-02-12
#include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; int main() { string s1, s2; vector<int> f, e; cin >> s1 >> s2; for (int i = 0; i < s1.size();i++) { //for(auto iter2=s2.begin();iter2!=s2.end();iter2++) if (s2[0] == s1[i]) f.push_back(i); if (s2[s2.size() - 1] == s1[i]) e.push_back(i); } int temp, temp2=1000; int res1=-1, res2=-1; for(int i=0;i<e.size();i++) for (int j = 0; j < f.size(); j++) { if (f[j] >= e[i]) continue; temp = e[i]-f[j]; if (temp+1 >= s2.size()&&temp<temp2) { res1 = f[j]; res2 = e[i]; } if(temp2>temp) temp2 = temp; } cout << res1<< ' ' << res2; return 0; }
小米筆試題,當時沒有加if(temp2>temp)這個判斷,只通過80%