2012年每周一赛第一场第二题。若然路径是闭合的,则整个路径是由若干个八边形构造而成,那么我们就可以把这些八边形逐一去掉,直至没有;若不能全部去掉,说明路径不是闭合的。至于去掉八边形的规则可以参考代码。
Run Time: 0sec
Run Memory: 312KB
Code Length: 775Bytes
Submit Time: 2012-02-27 23:23:42
// Problem#: 4833
// Submission#: 1222182
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <string>
using namespace std;
string from[ 9 ] = { "aa", "bb", "cc", "ababa", "babab", "acaca", "cacac", "bcbcb", "cbcbc" };
string to[ 9 ] = { "", "", "", "bab", "aba", "cac", "aca", "cbc", "bcb" };
int main()
{
int Z;
string s;
int i, j;
bool change;
cin >> Z;
while ( Z-- ) {
cin >> s;
do {
change = false;
for ( i = 0; i < 9; i++ ) {
if ( ( j = s.find( from[ i ] ) ) != string::npos ) {
s.erase( j, from[ i ].size() );
s.insert( j, to[ i ] );
change = true;
}
}
} while ( change );
cout << ( s.empty() ? "closed": "open" ) << endl;
}
return 0;
}