Topcoder SRM 597 DIV 1


250 LittleElephantAndString


class LittleElephantAndString {
	private:
        int ch[300];
	public:
	int getNumber(string A, string B) {
        int ans=0;
        int n=A.length();
        int p=n;
        memset(ch,0,sizeof(ch));
        for (int i=0;i<n;i++){
            ch[(int)A[i]]++;
        }
        for (int i=0;i<n;i++){
            ch[(int)B[i]]--;
        }
        for (int i=0;i<300;i++){
            if (ch[i]!=0) return -1;
        }
        for (int i=n-1;i>=0;i--){
            bool ok=false;
            for (int j=p-1;j>=0;j--){
                if (A[j]==B[i]){
                    ans++;
                    p=j;
                    ok=true;
                    break;
                }
            }
            if (!ok) break;
        }
        return n-ans;
	}
};




你可能感兴趣的:(Topcoder SRM 597 DIV 1)