杭电 2371 Decode the Strings

逃离我推掉我的手 2022-05-16 13:51 253阅读 0赞

题目链接:点我
Problem Description
Bruce Force has had an interesting idea how to encode strings. The following is the description of how the encoding is done:

Let x1,x2,…,xn be the sequence of characters of the string to be encoded.

  1. Choose an integer m and n pairwise distinct numbers p1,p2,…,pn from the set {1, 2, …, n} (a permutation of the numbers 1 to n).
  2. Repeat the following step m times.
  3. For 1 ≤ i ≤ n set yi to xpi, and then for 1 ≤ i ≤ n replace xi by yi.

For example, when we want to encode the string “hello”, and we choose the value m = 3 and the permutation 2, 3, 1, 5, 4, the data would be encoded in 3 steps: “hello” -> “elhol” -> “lhelo” -> “helol”.

Bruce gives you the encoded strings, and the numbers m and p1, …, pn used to encode these strings. He claims that because he used huge numbers m for encoding, you will need a lot of time to decode the strings. Can you disprove this claim by quickly decoding the strings?

Input
The input contains several test cases. Each test case starts with a line containing two numbers n and m (1 ≤ n ≤ 80, 1 ≤ m ≤ 109). The following line consists of n pairwise different numbers p1,…,pn (1 ≤ pi ≤ n). The third line of each test case consists of exactly n characters, and represent the encoded string. The last test case is followed by a line containing two zeros.

Output
For each test case, print one line with the decoded string.

Sample Input
5 3
2 3 1 5 4
helol
16 804289384
13 10 2 7 8 1 16 12 15 6 5 14 3 4 11 9
scssoet tcaede n
8 12
5 3 4 2 1 8 6 7
encoded?
0 0

Sample Output
hello
second test case
encoded?

用矩阵交换字母顺序,线性代数的知识点
要知道行列式的乘法规则
这里写图片描述
题目要我们逆推,求原矩阵的逆矩阵,详细实现见代码

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<cmath>
  6. #include<stack>
  7. #include<map>
  8. #include<vector>
  9. #include<queue>
  10. #include<set>
  11. #include<iomanip>
  12. #include<cctype>
  13. using namespace std;
  14. #define ll long long
  15. #define edl putchar('\n')
  16. #define sscc ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  17. #define FOR(i,a,b) for(int i=a;i<=b;i++)
  18. #define ROF(i,a,b) for(int i=a;i>=b;i--)
  19. #define FORLL(i,a,b) for(ll i=a;i<=b;i++)
  20. #define ROFLL(i,a,b) for(ll i=a;i>=b;i--)
  21. #define mst(a) memset(a,0,ssizeof(a))
  22. #define mstn(a,n) memset(a,n,ssizeof(a))
  23. #define zero(x)(((x)>0?(x):-(x))<eps)
  24. const int ssize=100;
  25. int n,m;
  26. int p[ssize],num[ssize];
  27. char s[ssize];
  28. struct Matrix {
  29. int a[ssize][ssize];
  30. Matrix() {
  31. memset(a,0,sizeof(a));
  32. }
  33. void init() {
  34. for(int i=0; i<n; i++)
  35. for(int j=0; j<n; j++)
  36. a[i][j]=(i==j);
  37. }
  38. Matrix operator + (const Matrix &B)const {
  39. Matrix C;
  40. for(int i=0; i<n; i++)
  41. for(int j=0; j<n; j++)
  42. C.a[i][j]=(a[i][j]+B.a[i][j]);
  43. return C;
  44. }
  45. Matrix operator * (const Matrix &B)const {
  46. Matrix C;
  47. for(int i=0; i<n; i++)
  48. for(int k=0; k<n; k++)
  49. for(int j=0; j<n; j++)
  50. C.a[i][j]=(C.a[i][j]+a[i][k]*B.a[k][j]);
  51. return C;
  52. }
  53. Matrix operator ^ (const ll &t)const {
  54. Matrix A=(*this),res;
  55. res.init();
  56. ll p=t;
  57. while(p) {
  58. if(p&1)res=res*A;
  59. A=A*A;
  60. p>>=1;
  61. }
  62. return res;
  63. }
  64. };
  65. int main() {
  66. int i;
  67. Matrix origin,A,ans;
  68. while(scanf("%d%d",&n,&m)!=EOF) {
  69. if(n==0&&m==0)
  70. break;
  71. for(i=1; i<=n; i++)
  72. scanf("%d",&num[i]);
  73. for(i=1; i<=n; i++) //取原矩阵的逆矩阵
  74. p[num[i]]=i;
  75. memset(origin.a,0,sizeof(origin.a));
  76. for(i=0; i<n; i++)
  77. origin.a[i][p[i+1]-1]=1;
  78. getchar();
  79. gets(s);
  80. origin=origin^m;
  81. memset(A.a,0,sizeof(A.a)); //原始序列[1 2 3 4 5 ...n]
  82. for(i=0; i<n; i++)
  83. A.a[i][0]=i;
  84. ans=origin*A; //与原始序列相乘
  85. for(i=0; i<n; i++)
  86. printf("%c",s[ans.a[i][0]]);
  87. // printf("%d ",ans.a[i][1]);
  88. printf("\n");
  89. }
  90. return 0;
  91. }

发表评论

表情:
评论列表 (有 0 条评论,253人围观)

还没有评论,来说两句吧...

相关阅读