P1 打倒怪獸 - Defeat the monster

 avatar
unknown
c_cpp
2 years ago
1.1 kB
11
Indexable
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
using std::string;

string n, m; //n:大摩導師的咒語,m:書的內容
bool point[1000000]={0}; //記憶相似字串的開頭
int p=0; //判斷有無符合資料的(用來輸出not found)
string dic[1000000];//用來排序

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    int N=n.size();   
    for(int i=0; i<m.size(); ++i){
        int f=0; //記錯誤次數
        for(int j=0; j< N && i+j<m.size(); ++j){	// 多判斷 i+j 是否合法 
            if(f>1) break; //錯誤1次以上,跳出	
            if(n[j] != m[i+j]) ++f; //有錯
            if(j==n.size()-1&&f<=1){ //咒語符合
                point[i]=1;
                ++p;
            }
        }
    }
    if(p==0){
        cout <<"Not found\n";
        return 0;
    }
    int s=0; 
    for(int i=0; i<m.size(); ++i){
        if(point[i]==1){
            dic[s]=m.substr(i,N); 
            ++s;
        }
    }
    sort(dic, dic+s);
    for(int i=0; i<s; ++i) cout << dic[i] << endl;
}
Editor is loading...