We call a string as a 0689-string if this string only consists of digits '0', '6', '8' and '9'. Given a 0689-string of length , one must do the following operation exactly once: select a non-empty substring of and rotate it 180 degrees.
More formally, let be the -th character in string . After rotating the substring starting from and ending at 180 degrees (), string will become string of length extracted from the following equation, where indicates the -th character in string :
What's the number of different strings one can get after the operation?
Input
There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:
The first and only line contains a 0689-string ().
It's guaranteed that the sum of of all test cases will not exceed .
Output
For each test case output one line containing one integer, indicating the number of different strings one can get after applying the operation exactly once.
Sample Input
2 0689 08
Sample Output
8 2
Hint
We hereby explain the first sample test case.
Substring | Result | Substring | Result | |
---|---|---|---|---|
0 | 0689 | 68 | 0899 | |
6 | 0989 | 89 | 0668 | |
8 | 0689 | 068 | 8909 | |
9 | 0686 | 689 | 0689 | |
06 | 9089 | 0689 | 6890 |
It's easy to discover that we can get 8 different strings after the operation.
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
long long int a,x,y,z,w;
long long int ans;
char s[1000010];
cin >> a;
while(a--){
ans=0;
x=0;
y=0;
z=0;
w=0;
cin >> s;
int len=strlen(s);
for(int i=0;i<len;i++){
if(s[i]=='0'){
x++;
}
if(s[i]=='8'){
y++;
}
if(s[i]=='6'){
z++;
}
if(s[i]=='9'){
w++;
}
ans+=i+1;
}
ans-=x*(x+1)/2;
ans-=y*(y+1)/2;
ans-=z*w;
if(z==len||w==len){
cout << ans << endl;
continue;
}
cout << ans+1 << endl;
}
return 0;
}
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
long long int a,x,y,z,w,t;
long long int ans;
char s[1000010];
cin >> a;
while(a--){
ans=0;
t=0;
x=0;
y=0;
z=0;
w=0;
cin >> s;
int len=strlen(s);
for(int i=0;i<len;i++){
if(s[i]=='0'){
ans+=(y+z+w);
x++;
}
if(s[i]=='8'){y++;
ans+=(x+z+w);
}
if(s[i]=='6'){
z++;
ans+=(x+y+z);
}
if(s[i]=='9'){
w++;
ans+=(x+y+w);
}
t=t+i+1;
}
if(ans<t){
ans++;
}
cout << ans << endl;
}
return 0;
}