Educational Codeforces Round 60 (Rated for Div. 2) E. Decypher the String

题目大意:这是一道交互题。给你一个长度为n的字符串,这个字符串是经过规则变换的,题目不告诉你变换规则,但是允许你提问3次:每次提问你给出一个长度为n的字符串,程序会返回按变换规则变换后的字符串,提问3次后你需要猜出这个字符串。解法是学习https://blog.csdn.net/baiyifeifei/article/details/87807822 这个博主的,借用了进制的思想非常巧妙。

解法:对于某个位置的来源位置我们设为x,因为26*26*26>10000,那么x可以唯一表示为x=a*26*26+b*26+c。那么我们只要想办法求出a,b,c就能得到它的来源位置。

那么怎么利用三次询问求a,b,c?我们首先构造(26*26个a) (26*26个b) (26*26个c)......(26*26个z),设此位置变换后的字符为c[0],那么a=c[0]-‘a‘(可以理解为a=x/(26*26)=c[0]-‘a‘)。道理类似的我们下一次构造(26个a) (26个b) (26个c) ......(26个d)的字符串让程序变换,此时变换后的字符为c[1],那么b=c[1]-‘a‘(可以理解为b=x%(26*26) /26=(b*26+c)/26=c[1]-‘a‘),这里的b可能有点儿难理解,为什么是这样得到的c[1]就是(b*26+c)/26?因为前一个询问我们已经确定x在a这一个(26*26)的大块里面了,那么我们只需要考虑这个大块,把这个大块再细分为26块看看返回什么值就能知道x在a这个大块的哪一个小块b里面。道理类似的最后构造(abc..z)(abc..z)...(abc..z)得到c=c[2]-‘a‘。

那么我们就得到该位置的来源是a*26*26+b*26+c,问题得到解决。

如果还是感觉难以理解,可以看看代码再仔细想想:

#include<bits/stdc++.h>
using namespace std;
const int N=10000+10;
int n;
char s[N],q[3][N],g[3][N],ans[N];

int main()
{
    scanf("%s",s);
    n=strlen(s);
    for (int i=0;i<n;i++) {
        q[0][i]=(i/(26*26)%26+‘a‘);
        q[1][i]=(i/26%26+‘a‘);
        q[2][i]=(i%26+‘a‘);
    } 

    for (int i=0;i<3;i++) {
        printf("? %s\n",q[i]);
        fflush(stdout);
        scanf("%s",g[i]);
    }
    for (int i=0;i<n;i++) {
        int tmp=(g[0][i]-‘a‘)*26*26+(g[1][i]-‘a‘)*26+(g[2][i]-‘a‘);
        ans[tmp]=s[i];
    }
    printf("! %s",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/clno1/p/10420899.html

时间: 2024-08-30 17:40:00

Educational Codeforces Round 60 (Rated for Div. 2) E. Decypher the String的相关文章

Educational Codeforces Round 60 (Rated for Div. 2)

A. Best Subsegment 题意 找 连续区间的平均值  满足最大情况下的最长长度 思路:就是看有几个连续的最大值 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn= 1e5+4; 4 int a[maxn]; 5 int main(){ 6 int n; 7 scanf("%d",&n); 8 int maxnum=0; 9 for(int i=0;i<n;i++)s

Educational Codeforces Round 60 (Rated for Div. 2) A. Best Subsegment

time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output You are given array a 1 , a 2 ,-, a n a1,a2,-,an . Find the subsegment a l , a l+1 ,-, a r al,al+1,-,ar ( 1≤l≤r≤n 1≤l≤r≤n ) with maximum arithmet

Educational Codeforces Round 60 (Rated for Div. 2) D. Magic Gems(矩阵快速幂)

题目传送门 题意: 一个魔法水晶可以分裂成m个水晶,求放满n个水晶的方案数(mol1e9+7) 思路: 线性dp,dp[i]=dp[i]+dp[i-m]; 由于n到1e18,所以要用到矩阵快速幂优化 注意初始化 代码: #include<bits/stdc++.h> using namespace std; #define mod 1000000007 typedef long long ll; #define MAX 105 const int N=105;//矩阵的大小 int T; ll

Educational Codeforces Round 60 (Rated for Div. 2)E(思维,哈希,字符串,交互)

#include <bits/stdc++.h>using namespace std;int main(){ string t; cin>>t; int n=t.size(); string s1(n,'a'),s2(n,'a'),s3(n,'a'); for(int i=0;i<n;i++){  s1[i]=char('a'+(i%26));//从a到z循环  s2[i]=char('a'+((i/26)%26));//第i位为(i/26)%26+'a',保证了26*26

Educational Codeforces Round 60 (Rated for Div. 2)D(思维,DP,快速幂)

#include <bits/stdc++.h>using namespace std;const long long mod = 1e9+7;unordered_map<long long,long long>mp;long long n,m;long long dp(long long n){    if(n<0)        return 0;    if(n<m)        return 1;    if(mp.find(n)!=mp.end())//已经

Educational Codeforces Round 81 (Rated for Div. 2) C. Obtain The String

题目链接:http://codeforces.com/contest/1295/problem/C 题目:给定字符串s,t.  给定一个空串z,需要按照规则把z构造成 string z == string t 的字符串. 规则:有限次从s中任取子序列p,然后进行 string z += string p的操作, s不变. 问能不能构造出p,不能输出-1,可以得话最少几次可以构造出. 大致的想法就是n倍的s字符串串联,然后剔除不必要的字符,就可以得出z, 求最少的倍数n是多少. 主要思路就是用二分

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is