forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathststring.cpp
69 lines (69 loc) · 914 Bytes
/
ststring.cpp
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
// 2008-10-22
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
void inc(char* s)
{
int cur=0;
bool ok=false;
while (!ok)
{
s[cur]++;
if (s[cur]=='K')
s[cur++]='A';
else
ok=true;
}
if (s[cur]==1)
{
s[cur]='A';
s[cur+1]=0;
}
}
int ok(char* s)
{
int i=1;
while (s[i])
{
if (s[i]==s[i-1]||s[i]==s[i-1]-1||s[i]==s[i-1]+1)
return 0;
i++;
}
return 1;
}
int hash(char* s)
{
int x=1;
int i=0;
int res=0;
while (s[i])
{
res+=x*(s[i++]-'A'+1);
x*=11;
}
return res;
}
int ans[2000000];
int main()
{
//precompute answers
char s1[10]="A",s2[10]="AAAAAAA";
int sum=0;
while (strcmp(s1,s2))
{
if (ok(s1))
sum++;
ans[hash(s1)]=sum;
inc(s1);
}
for(;;)
{
s1[0]=0;
scanf("%s %s",s1,s2);
if (!s1[0]) return 0;
reverse(s1,s1+strlen(s1));
reverse(s2,s2+strlen(s2));
printf("%d\n",max(ans[hash(s2)]-ans[hash(s1)]-ok(s2),0));
}
}