CDQZ_Training 2012-05-24 词编码
http://cdqz.openjudge.cn/noip/1009/
【题目描述】
一个发送机可以通过一条隧道发送一些以二进制代码组成的单词。在其尽头的接收机可以使用特殊技术恢复到最初的单词。每个单词最初都由0和1组成。所有的单词最初长上簦(上簦:度)都为n(4≤N ≤1000)。当穿过隧道之后单词可能发生以下几种情况之一:
(1)任意(一个)0被1取代
(2)任意(一个)符号被删除
(3)一个符号(0或1)被插入到任何位置
(4)不改变
我们知道最初的单词都具有以下性质:有1的位置号的总和是N+1的倍数,或者是0。
【input】
N和转换后的单词,每个单词占一行。单词数不大于2001.不会有其它任何东西,除了一些空格与空行,
【Output】
你的程序应该打印输出原始序列的词,注意换行。
若有多解,操作4优先,不行则按操作1,2,3优先。同一操作,按操作位置最先的优先。同一操作,按操作位置最先的优先(从左到右数起1,2,3,…N),还有操作2时,被删数列,先在被删数列添0,不行再添1。
如果没答案输出-1
【Sample input】
4
0000
011
1011
11011
【Sample Output】
0000
0110
1001
1111
1000ms/128000KB
题解:裸着模拟就OK了,再加个传说中的后缀处理,保证每种操作在O(n)的复杂度。顺便表示orz强大的std,各种不科学……
心得:先是各种wa,调了半节课终于过点了,然后就各种t。尝试了三种写法才AC,好吧。。。
话说以后考试神马的还是用心检查下,多试几组数据,尽量避免奇葩的错误。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 var 2 a:ansistring; 3 m,n,i,num:longint; 4 numa:array[1..1001]of longint; 5 procedure main(b:ansistring); 6 procedure outp(b:ansistring;point,instr:longint); 7 var 8 t:longint; 9 begin 10 for t:=1 to point-1 do 11 write(b[t]); 12 if instr<>0 then 13 if instr>0 then begin 14 if instr>1 then write('0') 15 else write('1'); 16 for t:=point to length(b) do write(b[t]); 17 end else 18 if instr<0 then 19 for t:=point+1 to length(b) do write(b[t]); 20 writeln; 21 end; 22 function right(j:longint):boolean; 23 begin 24 if (j mod (n+1)=0) then exit(True) else exit(False); 25 end; 26 procedure get(b:ansistring); 27 var 28 t,s:longint; 29 begin 30 num:=0; 31 s:=0; 32 for t:=length(b) downto 1 do begin 33 if b[t]='1' then begin 34 inc(num,t); 35 inc(s); 36 end; 37 numa[t]:=s; 38 end; 39 end; 40 function change(var b:ansistring):boolean; 41 var 42 t:longint; 43 begin 44 change:=False; 45 for t:=1 to length(b) do 46 if b[t]='1' then begin 47 dec(num,t); 48 if right(num) then begin 49 b[t]:='0'; 50 exit(True); 51 end else inc(num,t); 52 end; 53 end; 54 function delete(b:ansistring):boolean; 55 var 56 t,s:longint; 57 begin 58 delete:=False; 59 s:=num; 60 for t:=1 to length(b)+1 do begin 61 inc(num,numa[t]); 62 if right(num) then begin 63 outp(b,t,2); 64 exit(True); 65 end else 66 inc(num,t); 67 if right(num) then begin 68 outp(b,t,1); 69 exit(True); 70 end else 71 num:=s; 72 end; 73 end; 74 function insert(b:ansistring):boolean; 75 var 76 t,s:longint; 77 begin 78 insert:=False; 79 s:=num; 80 for t:=1 to length(b) do begin 81 dec(num,numa[t]); 82 if b[t]='1' then dec(num,t-1); 83 if right(num) then begin 84 outp(b,t,-1); 85 exit(True); 86 end else 87 num:=s; 88 end; 89 end; 90 var 91 s,t:longint; 92 begin 93 get(b); 94 s:=length(b); 95 if s=n then begin 96 if right(num) then outp(b,n+1,0) 97 else if change(b) then outp(b,n+1,0) 98 else writeln('-1'); 99 end else if s=n-1 then begin 100 if not delete(b) then writeln('-1'); 101 end else if s=n+1 then begin 102 if not insert(b) then writeln('-1'); 103 end else writeln('-1'); 104 end; 105 begin 106 readln(n); 107 while not eof do begin 108 fillchar(numa,sizeof(numa),0); 109 readln(a); 110 if a<>'' then (main(a)); 111 end; 112 end.