J - K ZOJ - 4128 (思维)

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.

SubstringResult SubstringResult
00689 680899
60989 890668
80689 0688909
90686 6890689
069089 06896890

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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZ --瑞 hopeACMer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值