USACO Section6.3 Cryptcowgraphy (cryptcow)

Problem Summary

Given a string T and an encrypted string S, determine whether S can be decoded as T.

The encryption of messege S in this problem goes like:
First, insert three letters C, O, and W, one after another and in random location in S.
Second, swap the part of S between C and O, and the part between O and W.

Note that this kind of encryption can be applied several times, and T does not contain any C, O, or W.

Solution

This problem can be solved with DFS, but it certainly needs some optimization.

First, we can examine the input S and count the numbers of all characters. If the numbers of C, O, and W are not the same, or the number of any other character in S is different from that in T, then S can not be decoded as T.

Note that, the string is cut into several sequences by C, O, and W. These sequences must be in T. If not, there is no need to continue the search.

Additionally, a tip about coding.
The C++ function ”memcpy“ requires the destination and source arrays to be seperate,i.e. not overlapping. For overlapping memory blocks, “memmove” is a safer approach.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
const int maxn = 100;
const char t[] = "Begin the Escape execution at the Break of Dawn";
char s[maxn],backup[maxn],tmp1[maxn],tmp2[maxn];
int n,s_count[300],t_count[300];
bool match;
void swap_s(int a,int b,int c,int len)
{
memmove(tmp1,s+a+1,b-1-a);
memmove(tmp2,s+b+1,c-1-b);
memmove(s+a,tmp2,c-1-b);
memmove(s+a+c-1-b,tmp1,b-1-a);
memmove(s+c-2,s+c+1,len-c-1);
s[len-3]=0;
}
bool my_strstr(char *p,int l)
{
bool flag;
for(int i=0;i<47;i++)
if(t[i]==p[0])
{
flag=true;
for(int j=0;j<l;j++)
if(t[i+j]!=p[j])
{
flag=false;
break;
}
if(flag) return true;
}
return false;
}
void dfs(int len,int num)
{
if(match || len<47) return;
if(num==0)
{
if(strcmp(s,t)==0)
match=true;
return;
}
int pos[3][10],idx[30];
pos[0][0]=pos[1][0]=pos[2][0]=idx[0]=0;
for(int i=0;i<len;i++)
if(s[i]=='O')
{
pos[0][0]++;
pos[0][pos[0][0]]=i;
idx[0]++;
idx[idx[0]]=i;
}
else
if(s[i]=='C')
{
pos[1][0]++;
pos[1][pos[1][0]]=i;
idx[0]++;
idx[idx[0]]=i;
}
else
if(s[i]=='W')
{
pos[2][0]++;
pos[2][pos[2][0]]=i;
idx[0]++;
idx[idx[0]]=i;
}
if(pos[1][1]>pos[0][1] || pos[1][1]>pos[2][1] || pos[2][num]<pos[0][1])
return;
if(pos[1][1]>0 && strncmp(s,t,pos[1][1])!=0) /* first segment */
return;
if(pos[2][1]<len && strcmp(s+pos[2][num]+1,t+48-len+pos[2][num])!=0) /* last segment */
return;
for(int i=0;i<idx[0];i++)
if(idx[i]+1<idx[i+1])
{
if(!my_strstr(s+idx[i]+1,idx[i+1]-idx[i]-1))
return;
}
char backup[maxn];
memcpy(backup,s,n+1);
for(int i=1;i<=num;i++)
for(int j=1;j<=num;j++)
if(pos[0][i]>pos[1][j])
for(int k=num;k>=1;k--)
{
if(pos[2][k]<=pos[0][i]) break;
swap_s(pos[1][j],pos[0][i],pos[2][k],len);
dfs(len-3,num-1);
memcpy(s,backup,n+1);
}
}
int main()
{
fstream fin("cryptcow.in",ios::in);
fstream fout("cryptcow.out",ios::out);
string s1;
getline(fin,s1);
fin.close();
n=s1.length();
for(int i=0;i<n;i++)
s[i]=s1[i];
s[n]=0;
for(int i=0;i<n;i++)
s_count[s[i]]++;
if(s_count['C']!=s_count['O'] || s_count['O']!=s_count['W'] || s_count['C']!=s_count['W'] || n-3*s_count['C']!=47)
{
fout<<"0 0"<<endl;
fout.close();
return 0;
}
if(s_count['C']==0)
{
if(strcmp(s,t)==0)
fout<<"1 0"<<endl;
else
fout<<"0 0"<<endl;
fout.close();
return 0;
}
for(int i=0;i<47;i++) t_count[t[i]]++;
bool flag=true;
for(int i=0;i<255;i++)
{
if(i=='C' || i=='O' || i=='W')
continue;
if(s_count[i]!=t_count[i])
{
flag=false;
break;
}
}
if(flag)
dfs(n,s_count['C']);
if(match)
fout<<"1 "<<s_count['C']<<endl;
else
fout<<"0 0"<<endl;
fout.close();
return 0;
}