UVA11489 题解

· · 题解

首先求出每一位对 3 取模的余数,然后求出先手要去掉什么数。

设这个数为 x,则看一下余数中是否存在 x\bmod 3,如果没有先手必输。

否则删掉 x 后,剩下的一定只能通过删余数为 0 的数来满足条件,所以如果余数为 0 的数的数量为奇数,先手必输。否则先手必胜。

时间复杂度 O(T|s|)。

AC code

#include<iostream>
using namespace std;
int T,a[4];
string s;
int main(){
    cin>>T;
    for(int c=1;c<=T;++c){
        a[0]=a[1]=a[2]=0;
        cin>>s;
        int l=s.size();
        for(int i=0;i<l;++i){
            a[(s[i]-'0')%3]++;//求出余数
        }
        int qwq=(a[1]+a[2]*2)%3;
        a[qwq]--;
        cout<<"Case "<<c<<": ";
        if(a[qwq]<0||a[0]&1)cout<<"T\n";
        else cout<<"S\n";
    } 
    return 0;
}