|
|
вернуться в форумWA test8! WA test8 WTF???!!! I use KMP help me plz code C++: #include <iostream> using namespace std; const long long max_length = 250000; char first_line[2 * max_length], second_line[max_length]; long long length; long long find() { long long i,j; char f[max_length]; f[0] = 0; f[1] = 0; for(i = 2; i <= length; i++) { j = f[i - 1]; for( ; ; ) { if(second_line[j] == second_line[i - 1]) { f[i] = j + 1; break; } if(j == 0) { f[i] = 0; break; } j = f[j]; } } i = 0; j = 0; for( ; ; ) { if(j == 2 * length - 1) break; if(first_line[j] == second_line[i]) { i++; j++; if(i == length) return length - (j - i) - 1; } else if(i > 0) i = f[i]; else j++; } return -1; } int main() { long long index; char temp[1]; cin >> length >> first_line >> second_line; temp[0] = first_line[0]; for (index = 0; index < length-1; index++) first_line[index] = first_line[index + 1]; first_line[length - 1] = temp[0]; for (index = 0; index < length - 1; index++) first_line[length + index] = first_line[index]; cout << find(); return 0; } Edited by author 21.03.2008 12:38 Edited by author 21.03.2008 12:38 Edited by author 21.03.2008 12:39 Re: WA test8! Someone knows what is that test? give me some tests please... |
|
|