D. Mahmoud and Ehab and the binary string Codeforces Round #435 (Div. 2)

http://codeforces.com/contest/862/problem/D

交互题

fflush(stdout)

调试:

先行给出结果,函数代替输入

  1 #include <cstdio>
  2 #include <cstdlib>
  3 #include <cmath>
  4 #include <cstring>
  5 #include <time.h>
  6 #include <string>
  7 #include <set>
  8 #include <map>
  9 #include <list>
 10 #include <stack>
 11 #include <queue>
 12 #include <vector>
 13 #include <bitset>
 14 #include <ext/rope>
 15 #include <algorithm>
 16 #include <iostream>
 17 using namespace std;
 18 #define ll long long
 19 #define minv 1e-6
 20 #define inf 1e9
 21 #define pi 3.1415926536
 22 #define E  2.7182818284
 23 const ll mod=1e9+7;//998244353
 24 const int maxn=1e3+10;
 25 
 26 char str[maxn];
 27 int pos[2],s,t,len=inf,x,y;
 28 char result[maxn]="01";
 29 int nn=2;
 30 
 31 int cal()
 32 {
 33     int i,v=0;
 34     for (i=1;i<=nn;i++)
 35         if (str[i]!=result[i-1])
 36             v++;
 37     return v;
 38 }
 39 
 40 //'l'-'r' must have num
 41 void work(int l,int r,int num)
 42 {
 43     if (l==r)
 44     {
 45         pos[num]=l;
 46         return;
 47     }
 48 
 49     int i,m=(l+r)>>1;
 50     for (i=l;i<=m;i++)
 51         str[i]=((str[i]-48) ^ 1)+48;
 52     printf("? %s
",str+1);
 53     fflush(stdout);
 54     scanf("%d",&x);
 55 //        x=cal();
 56 
 57     for (i=l;i<=m;i++)
 58         str[i]=((str[i]-48) ^ 1)+48;
 59 
 60     if (y-x==m-l+1)
 61     {
 62         pos[num]=l;
 63         return;
 64     }
 65     else
 66     {
 67         if (m-l+1<len)
 68         {
 69             len=m-l+1;
 70             s=l;
 71             t=m;
 72         }
 73     }
 74 
 75     if (x-y!=m-l+1)
 76         work(l,m,num);
 77     else
 78         work(m+1,r,num);
 79 }
 80 
 81 int main()
 82 {
 83     int n,i;
 84     scanf("%d",&n);
 85 
 86     for (i=1;i<=n;i++)
 87         str[i]='1';
 88     str[n+1]='';
 89     printf("? %s
",str+1);
 90     fflush(stdout);
 91     scanf("%d",&y);
 92 //        y=cal();
 93     work(1,n,0);
 94 
 95     if (len>0)
 96     {
 97         for (i=1;i<=n;i++)
 98             str[i]='0';
 99         str[n+1]='';
100         printf("? %s
",str+1);
101         fflush(stdout);
102         scanf("%d",&y);
103 //            y=cal();
104         if (len==inf)
105             s=1,t=n;
106         work(s,t,1);
107     }
108 
109     printf("! %d %d",pos[0],pos[1]);
110     return 0;
111 }
原文地址:https://www.cnblogs.com/cmyg/p/9521040.html