A题。。暴力枚举在每个位置添加字符,然后检查一下是不是回文串
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #include <vector> 7 8 using namespace std; 9 10 #define LL long long 11 #define eps 1e-8 12 #define inf 0x3f3f3f3f 13 #define lson l, m, rt << 1 14 #define rson m+1, r, rt << 1 | 1 15 #define mnx 31000 16 17 char s[20], ch[20]; 18 bool check(){ 19 int n = strlen( ch ); 20 for( int i = 0, j = n-1; i <= j; ++i, --j ){ 21 if( ch[i] != ch[j] ) return false; 22 } 23 return true; 24 } 25 int main(){ 26 scanf( "%s", &s ); 27 int n = strlen( s ); 28 for( int i = 0; i <= n; ++i ){ 29 for( int j = ‘a‘; j <= ‘z‘; ++j ){ 30 for( int k = 0, m = 0; k <= n; ++k ){ 31 if( i == k ) 32 ch[k] = (char)j; 33 else 34 ch[k] = s[m++]; 35 } 36 ch[n+1] = ‘\0‘; 37 if( check() ){ 38 printf( "%s\n", ch ); 39 return 0; 40 } 41 } 42 } 43 puts( "NA" ); 44 return 0; 45 }
B题。。也是暴力枚举每种颜色,dfs算一下就好
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #include <vector> 7 8 using namespace std; 9 10 #define LL long long 11 #define eps 1e-8 12 #define inf 0x3f3f3f3f 13 #define lson l, m, rt << 1 14 #define rson m+1, r, rt << 1 | 1 15 #define mnx 500 16 17 int fst[mnx], nxt[mnx], vv[mnx], col[mnx], e, ans; 18 void add( int u, int v, int c ){ 19 vv[e] = v, col[e] = c, nxt[e] = fst[u], fst[u] = e++; 20 } 21 bool vis[mnx], ok; 22 void dfs( int u, int gg, int vol ){ 23 if( u == gg ){ 24 ans++; ok = 1; return ; 25 } 26 if( vis[u] ) return ; 27 vis[u] = 1; 28 for( int i = fst[u]; i != -1; i = nxt[i] ){ 29 if( ok ) return ; 30 int v = vv[i], c = col[i]; 31 if( c != vol ) continue; 32 dfs( v, gg, vol ); 33 } 34 } 35 int main(){ 36 int n, m, q; 37 scanf( "%d %d", &n, &m ); 38 memset( fst, -1, sizeof( fst ) ); 39 for( int i = 0; i < m; ++i ){ 40 int u, v, c; 41 scanf( "%d %d %d", &u, &v, &c ); 42 add( u, v, c ); 43 add( v, u, c ); 44 } 45 scanf( "%d", &q ); 46 while( q-- ){ 47 int u, v; 48 scanf( "%d%d", &u, &v ); 49 ans = 0; 50 for( int i = 1; i <= m; ++i ){ 51 memset( vis, 0, sizeof( vis ) ); 52 ok = 0; 53 dfs( u, v, i ); 54 } 55 cout << ans << endl; 56 } 57 return 0; 58 }
C题。。比赛的时候想不出来,看了题解才知道 第二维的状态最多不超过500。。因为你1+2+...+250 > 3w,这样每次步数减一或者加一的总的状态不会超过500,所以dp[30000][600]就够了, 把第二维300当做第一次的步数d,然后每次有可能走 d+j-n - 1, d+j-n, d+j-n+1步,感觉看代码容易理解一些。。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #include <vector> 7 8 using namespace std; 9 10 #define LL long long 11 #define eps 1e-8 12 #define inf 0x3f3f3f3f 13 #define lson l, m, rt << 1 14 #define rson m+1, r, rt << 1 | 1 15 #define mnx 31000 16 17 const int N = 300; 18 int dp[mnx][700], val[mnx]; 19 int main(){ 20 int n, m; 21 while( scanf( "%d%d", &n, &m ) != EOF ){ 22 memset( val, 0, sizeof val ); 23 memset( dp, 0, sizeof dp ); 24 for( int i = 0; i < n; ++i ){ 25 int x; 26 scanf( "%d", &x ); 27 val[x]++; 28 } 29 dp[m][N] = val[m] + val[0] + 1; 30 int ans = 0; 31 for( int i = m; i < mnx; ++i ){ 32 for( int j = 0; j < 600; ++j ){ 33 if( !dp[i][j] ) continue; 34 int l = m + j - N; 35 ans = max( dp[i][j] - 1, ans ); 36 if( i + l >= mnx ) continue; 37 if( l > 0 ) 38 dp[i+l][j] = max( dp[i+l][j], dp[i][j] + val[i+l] ); 39 if( l > 1 ) 40 dp[i+l-1][j-1] = max( dp[i+l-1][j-1], dp[i][j] + val[i+l-1] ); 41 if( l >= 0 ) 42 dp[i+l+1][j+1] = max( dp[i+l+1][j+1], dp[i][j] + val[i+l+1] ); 43 } 44 } 45 cout << ans << endl; 46 } 47 return 0; 48 }
D题。。好像是B的加强版,用并查集搞。。明天再看看
时间: 2024-10-10 13:53:28