Last active
May 26, 2016 06:10
-
-
Save yakimelon/cca858d53bde98548d0983bd6d9e5885 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
int main(void){ | |
char str[256],pat[256]; | |
strcpy(str,"chino-chan"); | |
strcpy(pat,"nd"); | |
int str_len,pat_len; // 文字サイズ | |
int str_count,pat_count; // カウンタ | |
int index=0; // 文字の比較位置 | |
int move_size; // 移動量 | |
int match_count=0; // 一致した数 | |
int flag = 0; // 一致フラグ | |
// 文字サイズ取得 | |
str_len = strlen(str); | |
pat_len = strlen(pat); | |
// 初期化 | |
str_count = 0; | |
// TODO:ネストが深いぃ | |
index = pat_len - 1; | |
printf("index=%d\n",index); | |
while( index < str_len ){ | |
for( pat_count = pat_len - 1, str_count = 0 ; pat_count >= 0 ; pat_count--, str_count++ ){ | |
// 文字列とパターンが一致しなかったら移動 | |
printf("str = %c pat = %c\n",str[index-str_count],pat[pat_count]); | |
if( str[index - str_count] != pat[pat_count] ){ | |
match_count = 0; | |
// 対象文字列のインデックスがパターンに含まれるか | |
for( pat_count = pat_len - 1 ; pat_count >= 0 ; pat_count-- ){ | |
printf("%d\n",pat_count); | |
printf("pat = %c str = %c\n",pat[pat_count],str[index-str_count]); | |
if( pat[pat_count] == str[index-str_count] ){ | |
// 一致した文字列の場所まで移動 | |
move_size = pat_len - 1 - pat_count; | |
break; | |
} | |
} | |
// 含まれなければ文字列分移動 | |
if( pat_count < 0 ) move_size = pat_len; | |
printf("move_size = %d\n",move_size); | |
break; | |
}else{ | |
match_count++; | |
} | |
} | |
printf("match_count = %d pat_len = %d\n",match_count,pat_len); | |
// 一致すれば一致と表示 | |
if( match_count == pat_len ){ | |
printf("一致\n"); | |
flag = 1; | |
break; | |
}else{ | |
// 一致していなければインデックス移動 | |
index += move_size; | |
} | |
printf("index = %d str_len = %d\n\n",index,str_len); | |
} | |
if( flag == 0 ) printf("不一致\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment