#32569: 卡在第二測資 NA (score:88%)


alex950301 (alex0301)

學校 : 新北市立新莊高級中學
編號 : 141423
來源 : [61.64.1.159]
最後登入時間 :
2024-05-03 23:38:53
c929. 蝸牛老師的點名單-續 | From: [210.71.72.105] | 發表日期 : 2022-10-21 11:40

 

#include <iostream>
using namespace std;
int main(){
    string a , b ;
    getline( cin , a ) ; 
    getline( cin , b ) ;
    char c[b.size()+2][a.size()] ;
    for( int j = 0 ; j < b.size() - 2 ; j++ ) {   //用c二維陣列儲存b-2個a.size()大小的字元
        int flag = 0 ;
        int J = j ;
        for( int I = 0 ; I < a.size() ; I++ ) {
            c[j][I] = char(b[J]) ;
            J++ ;
        }
    }
    for( int j = 0 ; j < b.size() - 2 ; j++ ) {  //如果檢測到字串中有地方符合把b[i]設為-1
        bool flag = 1 ;
        for( int I = 0 ; I < a.size() ; I++ ) {
            if( c[j][I] != a[I] ) flag = 0 ;
        }
        
        if( flag == 1 ) {
            for( int i = 0 ; i < a.size() ; i++ ) b[j+i] = -1 ;
        }
    }
    int n = 0 ;
    for( int i = 0 ; i < b.size() ; i++ ) {    //如果連續a.size()個字元都是-1->輸出,否則輸出b[i]字元
        if( b[i] != -1 ) cout << b[i] ;
        else n++ ;
        
        if( n == a.size() ) {
            cout << endl ;
            n = 0 ;
        }
    }
    cout << endl ;
    
    return 0;
}

不知道錯哪裡~~

 
#32574: Re: 卡在第二測資 NA (score:88%)


alex950301 (alex0301)

學校 : 新北市立新莊高級中學
編號 : 141423
來源 : [61.64.1.159]
最後登入時間 :
2024-05-03 23:38:53
c929. 蝸牛老師的點名單-續 | From: [61.64.1.159] | 發表日期 : 2022-10-21 23:50

 

可以試著用兩個for解決,以測資1作範例:

分別比對 and  和   appleandbanana

 

app and

ppl and

ple and

.

.

.

and and (可以建立一個n儲存有幾個相同,如果 n = a.size()  => 輸出endl ,b[i] 中的 i 要加上 a.size() - 1 ,避免重複輸出)

.

.

ana and

 

如果以上敘述很攏統,在下面附上參考解答

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

#include <iostream>
using namespace std;
int main(){
    string a , b ;
    getline( cin , a ) ; 
    getline( cin , b ) ;
    for( int i = 0 ; i < b.size() ; i++ ) {
        int I = i , n = 0 ; 
        for( int j = 0 ; j < a.size() ; j++ ) {
            if( b[I] == a[j] ) n++ ;
            I++ ;
        }
        if( n == a.size() ) {
            cout << endl ;
            i += a.size() - 1 ;
        }
        else cout << b[i] ;
    }
    cout << endl ;
    return 0;
}
 
#32587: Re: 卡在第二測資 NA (score:88%)


cges30901 (cges30901)

學校 : 不指定學校
編號 : 30877
來源 : [101.136.203.77]
最後登入時間 :
2024-04-07 15:34:14
c929. 蝸牛老師的點名單-續 | From: [27.53.233.124] | 發表日期 : 2022-10-22 19:46


既然都用string了,直接b.find(a)就好了

 
#32591: Re: 卡在第二測資 NA (score:88%)


0301alex (alex)

學校 : 不指定學校
編號 : 94037
來源 : [61.64.1.159]
最後登入時間 :
2024-04-09 19:58:33
c929. 蝸牛老師的點名單-續 | From: [61.64.1.159] | 發表日期 : 2022-10-22 23:43

噗!XD寫了個寂寞

 
ZeroJudge Forum