Codeforces-1117E:Decypher the String(思维)

版权声明:http://blog.csdn.net/Mitsuha_。 https://blog.csdn.net/Mitsuha_/article/details/87794531

E. Decypher the String
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.

You are given a string t consisting of n lowercase Latin letters. This string was cyphered as follows: initially, the jury had a string s consisting of n lowercase Latin letters. Then they applied a sequence of no more than n (possibly zero) operations. i-th operation is denoted by two integers a i a_i and b i b_i ( 1 a i , b i n ) (1≤a_i,b_i≤n) , and means swapping two elements of the string with indices ai and bi. All operations were done in the order they were placed in the sequence. For example, if s is xyz and 2 following operations are performed: a 1 = 1 , b 1 = 2 ; a 2 = 2 , b 2 = 3 a_1=1,b_1=2; a_2=2,b_2=3 , then after the first operation the current string is yxz, and after the second operation the current string is yzx, so t is yzx.

You are asked to restore the original string s. Unfortunately, you have no information about the operations used in the algorithm (you don’t even know if there were any operations in the sequence). But you may run the same sequence of operations on any string you want, provided that it contains only lowercase Latin letters and its length is n, and get the resulting string after those operations.

Can you guess the original string s asking the testing system to run the sequence of swaps no more than 3 times?

The string s and the sequence of swaps are fixed in each test; the interactor doesn’t try to adapt the test to your solution.

Input
Initially the testing system sends one string t, consisting of lowercase Latin letters ( 1 t = n 1 0 4 ) (1≤|t|=n≤10^4) .

Output
To give the answer, your program should print one line ! s with a line break in the end. After that, it should flush the output and terminate gracefully.

Interaction
Before giving the answer, you may submit no more than 3 queries. To ask a query, print one line in the following format: ? s′, where s′ should be a string consisting of exaclty n lowercase Latin letters. The line should be ended with a line break character. After submitting a query, flush the output and read the answer to your query — a string t′ consisting of n lowercase Latin letters, which is the result of applying the sequence of swaps to string s′. This string will be given on a separate line ended by a line break character.

If you submit an incorrect query (or ask more than 3 queries), the answer to it will be one string 0. After receiving such an answer, your program should terminate immediately — otherwise you may receive verdict “Runtime error”, “Time limit exceeded” or some other verdict instead of “Wrong answer”.

Example
input
yzx
aab
baa
aba
output
? baa
? aba
? aab
! xyz

思路:如果有n个字符每个都是独一无二,没有重复的,那么就很简单了,但是题目要求只能是小写字母。

我们可以把这n个字符的下标拿去询问,并且每个位置的下标都是不一样的,那么如何把下标转化为字符呢?
把这些下标化为26进制数,因为 2 6 3 > n 26^3>n ,所以每个下标化为26进制数后的长度不会超过3,然后我们把26进制数中的每一位变为小写字母,分3次进行询问,最后再组合起来,就可以得到原交换操作了。

#include<bits/stdc++.h>
using namespace std;
const int MOD=1e9+7;
const int MAX=1e5+10;
typedef long long ll;
char s[MAX],A[MAX];
int t[MAX],rev[MAX];
void cal()
{
    printf("? %s\n",A+1);
    fflush(stdout);
    scanf("%s",A+1);
}
int main()
{
    scanf("%s",s+1);
    int n=strlen(s+1);
    for(int i=1;i<=26*26;i=i*26)
    {
        for(int j=1;j<=n;j++)A[j]=(j/i)%26+'a';
        cal();
        for(int j=1;j<=n;j++)t[j]+=(A[j]-'a')*i;
    }
    for(int i=1;i<=n;i++)rev[t[i]]=i;
    printf("! ");
    for(int i=1;i<=n;i++)cout<<s[rev[i]];
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Mitsuha_/article/details/87794531