Keyword: search, ad hoc.
Algorithm:
Use a for loop to iterate through all days.
For each day, check if he can recognize that person. You may use another for loop to check that.
After each day, update the answer, and the answer after the last day is what you want.
Code:
#include<cstdio> #include<cstring> #include<algorithm> int main() { int t, day, mem; char man[ 505 ], *found; scanf( "%d", &t ); for( int i = 1; i <= t; ++i ) { int reg = 0; scanf( "%d %d %s", &day, &mem, man ); for( int i = 0; i < day; ++i ) { found = std::find( man + ( i - mem >= 0? i - mem : 0 ), man + i, man[ i ] ); if( found != man + i ) ++reg; } printf( "Case %d: %d\n", i, reg ); } return 0;
}
沒有留言:
張貼留言