次のコードは、2つの文字列に共通の部分文字列があるかどうかを確認するために使用されます。 No:7の行はどうしますか?説明してください。BIT操作を使用して2つの文字列に共通部分文字列があるかどうかを確認します。
1 #include<iostream>
2 using namespace std;
3
4 int letterBits(const string &s) {
5 int bits = 0;
6 for (char ch : s)
7 bits |= 1 << (ch - 'a');
8 return bits;
10 }
11
12 int main() {
13 int testCases;
14 cin >> testCases;
15 while (testCases--) {
16 string strA, strB;
17 cin >> strA >> strB;
18 int bitsA = letterBits(strA);
19
20 int bitsB = letterBits(strB);
21 cout<<bitsB<<" ";
22 cout << (bitsA & bitsB ? "YES": "NO") << endl;
23 }
24 return 0;
25 }
Woeは、 '(ch - 'a')'に対して負の結果や大きすぎる結果を生成する文字列を入力した人は誰でも...これは良い方法ではありません。 –
良いフォーマットと行番号をありがとう。 +1 –
注意してください。行7のセマンティクスでは、 '<< '演算子はストリーム挿入ではなく、左シフト*です。 –