A.迴文串
#include <cstdio>
#include <cstring>
bool is_palin( char *s){
int x = strlen( s);
for ( int i = 0; i < ( x - 1) / 2; i++){
if ( s[i] != s[x - i - 1])
return 0;
}
return 1;
}
char a[10010], b[10010];
int main()
{
int t;
scanf( "%d", &t);
while ( t--){
scanf( "%s", a);
int x = strlen( a), i;
for ( i = 0; i < x; i++){
strncpy( b, a, i);
strcpy( b + i, a + i + 1);
if ( is_palin( b)){
break;
}
}
if ( i == x && !is_palin( a))
printf( "No\n");
else
printf( "Yes\n");
}
return 0;
}
水題一個,直接判斷刪除對應各位(將刪除後的串保留到另外數組中)後是否是迴文串即可B: 路上的顏色
代碼如下:#include <cstdio>
#define maxn 105
int pa[maxn][maxn];
int findpa( int x, int y){
return pa[y][x] == x ? x : pa[y][x] = findpa( pa[y][x], y);
}
void unionpa( int x, int y, int z){
int fx = findpa( x, z), fy = findpa( y, z);
if ( fx != fy) pa[z][fy] = fx;
}
int main()
{
int t, n, m, a, b, c, q;
scanf( "%d", &t);
while ( t--){
scanf( "%d%d", &n, &m);
int x = m;
for ( int i = 1; i <= n; i++){
for ( int j = 1; j <= m; j++)
pa[j][i] = i;
}
while ( x--){
scanf( "%d%d%d", &a, &b, &c);
unionpa( a, b, c);
}
scanf( "%d", &q);
while ( q--){
scanf( "%d%d", &a, &b);
int s = 0;
for ( int i = 1; i <= m; i++){
if ( findpa( a, i) == findpa( b, i)){
s++;
}
}
printf( "%d\n", s);
}
}
return 0;
}
對於每種顏色維護一個並查集,讀入邊時合併,查詢時一次遍歷判斷對應集合代表元素是否相同即可
C: Flower
代碼如下#include <cstdio>
int main()
{
int t, r, g, b, x, y, z;
scanf( "%d", &t);
while ( t--){
scanf( "%d%d%d", &r, &g, &b);
int s = ( x = r / 3) + ( y = g / 3) + ( z = b / 3);
r = r % 3;
g = g % 3;
b = b % 3;
if ( r == 2 && g == 2 && b == 2){
s += 2;
}
else if ( r >= 1 && g >= 1 && b >= 1)
s++;
if ( r == 0 && g == 2 && b == 2 && x > 0 || r == 2 && g == 0 && b == 2
&& y > 0 || r == 2 && g == 2 && b == 0 && z > 0)
s++;
printf( "%d\n", s);
}
return 0;
}
水題,注意一下特殊情況即可( 餘數0 2 2 , 2 2 2。。。)。注:OJ上的數據貌似有點弱,有組數據當時我自己測沒過然後提交過了。。。
D: Ringo和機器人
#include <cstdio>
#include <cstring>
char a[110];
int main()
{
int t;
long long x, y;
scanf( "%d", &t);
while ( t--){
scanf( "%lld%lld", &x, &y);
scanf( "%s", a);
int w = strlen( a), s = 0, d = 0;
for ( int i = 0; i < w; i++){
if ( a[i] == 'L')
s--;
else if ( a[i] == 'R')
s++;
else if ( a[i] == 'U')
d++;
else if ( a[i] == 'D')
d--;
}
int l = 0, r = 0, i;
if ( x == 0 && y == 0)
printf( "Yes\n");
else{
for ( i = 0; i < w; i++){
if ( a[i] == 'L')
l--;
else if ( a[i] == 'R')
l++;
else if ( a[i] == 'U')
r++;
else if ( a[i] == 'D')
r--;
if ( s != 0 && d != 0 && ( x - l) / s == ( y - r) / d && ( x - l) / s >= 0 && ( x - l) % s == 0 && ( y - r) % d == 0)
break;
else if ( s != 0 && d == 0 && ( x - l) % s == 0 && ( y - r) == 0 && ( x - l) / s >= 0)
break;
else if ( s == 0 && d != 0 && ( x - l) == 0 && ( y - r) % d == 0 && ( y - r) / d >= 0)
break;
else if ( s == 0 && d == 0 && ( x - l) == 0 && ( y - r) == 0)
break;
}
if ( i == w)
printf( "No\n");
else
printf( "Yes\n");
}
}
return 0;
}
首先算出執行完整一遍指令的x,y的變化,然後一次遍歷,注意對各種情況的分類即可
E: 紙牌
#include <cstdio>
#include <cmath>
int main()
{
int t, n, x, y;
scanf( "%d", &t);
while ( t--){
scanf( "%d%d", &n, &x);
int s = 0;
while ( n--){
scanf( "%d", &y);
s += y;
}
s = abs( s);
y = s / x;
if ( s % x)
y++;
printf( "%d\n", y);
}
return 0;
}
最水的題,沒啥說的,五分鐘應該能切掉