B.Cuckoo for Hashing
模拟题。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <string> #include <vector> using namespace std; #define N 50007 int a[1004],b[1004]; int main() { int n1,n2,m,i; int x,fx,tx; int tmp,tmp2; int cs = 1; while(scanf("%d%d%d",&n1,&n2,&m)!=EOF && (n1||n2||m)) { memset(a,-1,sizeof(a)); memset(b,-1,sizeof(b)); while(m--) { scanf("%d",&x); fx = x%n1; tx = 1003; if(a[fx] == -1) { a[fx] = x; continue; } tmp2 = x; while(a[fx] != -1) { tmp = a[fx]; a[fx] = tmp2; tx = tmp%n2; if(b[tx] != -1) { tmp2 = b[tx]; b[tx] = tmp; fx = tmp2%n1; } else { b[tx] = tmp; break; } } if(a[fx] == -1) a[fx] = tmp2; } printf("Case %d:\n",cs++); int flag = 0; //printf("Table 1\n"); for(i=0;i<n1;i++) { if(a[i] != -1) { flag = 1; break; } } if(flag) { printf("Table 1\n"); for(i=0;i<n1;i++) { if(a[i] != -1) printf("%d:%d\n",i,a[i]); } } flag = 0; for(i=0;i<n2;i++) { if(b[i] != -1) { flag = 1; break; } } if(flag) { printf("Table 2\n"); for(i=0;i<n2;i++) { if(b[i] != -1) printf("%d:%d\n",i,b[i]); } } } return 0; }
C.Playing Fair with Cryptography
模拟题,注意细节就好。遇到J的情况要及时跳走。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <string> using namespace std; #define N 50007 char mp[7][7]; char key[100003],text[100004]; int vis[28]; string RUN(string ss) { int i,j; int a,b; int c,d; string ans = ""; for(i=0;i<5;i++) { for(j=0;j<5;j++) { if(mp[i][j] == ss[0]) a = i,b = j; if(mp[i][j] == ss[1]) c = i,d = j; } } if(a == c) { int newb = (b+1)%5; ans += mp[a][newb]; int newd = (d+1)%5; ans += mp[a][newd]; return ans; } else if(b == d) { int newa = (a+1)%5; ans += mp[newa][b]; int newc = (c+1)%5; ans += mp[newc][b]; return ans; } else { int newb = d; int newd = b; ans += mp[a][newb]; ans += mp[c][newd]; return ans; } } int main() { int t,i,cs = 1,j,k; int x,y; scanf("%d",&t); getchar(); while(t--) { gets(key); gets(text); int len1 = strlen(text); int len2 = strlen(key); memset(vis,0,sizeof(vis)); k = 0; for(i=0;i<len2;i++) { char ch = key[i]; if((ch >= 'A' && ch <= 'Z')) { if(vis[ch-'A']) continue; vis[ch-'A'] = 1; mp[k/5][k%5] = ch; k++; } else if(ch >= 'a' && ch <= 'z') { ch -= 32; if(vis[ch-'A']) continue; vis[ch-'A'] = 1; mp[k/5][k%5] = ch; k++; } } for(char chh = 'A';chh<='Z';chh++) { if(chh == 'J') continue; if(!vis[chh-'A']) { mp[k/5][k%5] = chh; k++; } } //alpha table established char ss[100004]; k = 0; for(i=0;i<len1;i++) { if(text[i] >= 'A' && text[i] <='Z') ss[k++] = text[i]; else if(text[i] >= 'a' && text[i] <= 'z') ss[k++] = text[i]-32; } ss[k] = '\0'; printf("Case %d: ",cs++); int nowch = 0; char nowchar; string cy = ""; for(i=0;i<k-1;i+=2) { if(nowch == 9) nowch++,nowch%=26; if(ss[i] == ss[i+1]) { if(ss[i] == (nowch%26) + 'A') { nowchar = (++nowch)%26 + 'A'; if(nowchar == 'J') nowch++,nowchar = 'K'; } nowchar = (nowch%26) + 'A'; if(nowchar == 'J') nowch++,nowchar = 'K'; cy = ""; cy += ss[i]; cy += nowchar; cout<<RUN(cy); nowch = (nowch+1)%26; i -= 1; } else { cy = ""; cy += ss[i]; cy += ss[i+1]; cout<<RUN(cy); } } if(i == k-1) { nowchar = nowch%26 + 'A'; if(ss[i] == nowchar) { nowchar = (++nowch)%26 + 'A'; } if(nowchar == 'J') nowch++,nowchar = 'K'; cy = ""; cy += ss[i]; cy += nowchar; cout<<RUN(cy); } cout<<endl; } return 0; }
H.The Urge to Merge
DP。借鉴标程代码。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <string> #define Mod 1000000007 using namespace std; #define N 50007 int mp[3][1003]; ll dp[8][1004]; ll Max(ll a,ll b,ll c,ll d) { return max(max(max(a,b),c),d); } int main() { int cs = 1,n; int i,j,k; while(scanf("%d",&n)!=EOF && n) { for(i=0;i<3;i++) for(j=1;j<=n;j++) scanf("%d",&mp[i][j]); //memset(dp,0,sizeof(dp)); for(i=0;i<7;i++) dp[i][0] = dp[i][1] = 0; dp[4][1] = mp[0][1]*mp[1][1]; dp[6][1] = mp[1][1]*mp[2][1]; ll k1 = Max(dp[4][1],dp[6][1],0,0); ll k2 = 0; ll s1,s2,s3,s4,s5; for(j=2;j<=n;j++) { s1 = mp[0][j-1]*mp[0][j]; s2 = mp[1][j-1]*mp[1][j]; s3 = mp[2][j-1]*mp[2][j]; s4 = mp[0][j]*mp[1][j]; s5 = mp[1][j]*mp[2][j]; dp[1][j] = Max(k2,dp[2][j-1],dp[3][j-1],dp[6][j-1])+s1; //第一种组合 dp[2][j] = Max(k2,dp[1][j-1],dp[3][j-1],dp[5][j-1])+s2; //第二种组合 dp[3][j] = Max(k2,dp[1][j-1],dp[2][j-1],dp[4][j-1])+s3; //第三种组合 dp[4][j] = Max(k2+s1+s2,dp[3][j-1]+s1+s2,k1+s4,0); dp[5][j] = Max(k2,dp[2][j-1],0,0)+s1+s3; dp[6][j] = Max(k2+s2+s3,dp[1][j-1]+s2+s3,k1+s5,0); dp[0][j] = Max(k2+s1+s2+s3,dp[1][j]+s5,dp[3][j]+s4,0); k2 = k1; for(i=0;i<7;i++) k1 = max(k1,dp[i][j]); } ll ans = -Mod; for(i=0;i<7;i++) ans = max(ans,dp[i][n]); printf("Case %d: %d\n",cs++,ans); } return 0; }
(没做出来的以后持续更新)