凯撒密码 CH Round #57 - Story of the OI Class
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2357%20-%20Story%20of%20the%20OI%20Class/凯撒密码
题解:刚开始想map,结果被出题说的卡map提醒了。
然后直觉告诉我可以hash相邻字母的距离,然后就这样做了。。。
代码:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set> 10 #include<queue> 11 #include<string> 12 #define inf 1000000000 13 #define maxn 1000000 14 #define maxm 500+100 15 #define eps 1e-10 16 #define ll long long 17 #define pa pair<int,int> 18 #define for0(i,n) for(int i=0;i<=(n);i++) 19 #define for1(i,n) for(int i=1;i<=(n);i++) 20 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 21 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 22 #define mod 1000000007 23 using namespace std; 24 inline int read() 25 { 26 int x=0,f=1;char ch=getchar(); 27 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 28 while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();} 29 return x*f; 30 } 31 int n,t,a[maxn],b[maxn],f[50][50]; 32 char s[10]; 33 int main() 34 { 35 freopen("input.txt","r",stdin); 36 freopen("output.txt","w",stdout); 37 n=read(); 38 for1(i,26) 39 for1(j,26) 40 f[i][j]=j>=i?j-i:26-i+j; 41 for1(i,n) 42 { 43 scanf("%s",s);t=0; 44 for0(j,3)t=t*26+f[s[j]-'a'+1][s[j+1]-'a'+1]; 45 a[t]=i;b[t]=s[0]; 46 } 47 for1(i,n) 48 { 49 scanf("%s",s);t=0; 50 for0(j,3)t=t*26+f[s[j]-'a'+1][s[j+1]-'a'+1]; 51 printf("%d %d\n",a[t],f[s[0]-'a'+1][b[t]-'a'+1]); 52 } 53 return 0; 54 }