def solve(): n = int(input()) s = input() cnt = set() for i in range(1, n): cnt.add(s[i - 1] + s[i]) print(len(cnt)) t = int(input()) for _ in range(t): solve() 把这段代码翻译为c++
时间: 2024-02-14 16:29:43 浏览: 31
Sure! Here's the translated code in C++:
```cpp
#include <iostream>
#include <unordered_set>
using namespace std;
void solve() {
int n;
cin >> n;
string s;
cin >> s;
unordered_set<string> cnt;
for (int i = 1; i < n; i++) {
cnt.insert(s.substr(i - 1, 2));
}
cout << cnt.size() << endl;
}
int main() {
int t;
cin >> t;
for (int i = 0; i < t; i++) {
solve();
}
return 0;
}
```
In this C++ translation, we use `unordered_set` from the `<unordered_set>` library to store unique pairs of adjacent characters. We also use `cin` and `cout` for input and output, respectively.
相关问题
import collections def solve_method(n: int) -> None: asc = True list_ = collections.deque() x = 1 for i in range(1, n + 1): arr = [0] * i if asc: for j in range(i): arr[j] = x x += 1 else: for j in range(i - 1, -1, -1): arr[j] = x x += 1 list_.appendleft(arr) asc = not asc res = [] head = "" for ints in list_: content = head for j in range(len(ints)): num = ints[j] content += str(num) content += "*" * (4 - len(str(num))) if j != len(ints) - 1: content += " " res.append(content) head += " " for s in reversed(res): print(s) if __name__ == "__main__": n = int(input()) solve_method(n) 改写以上代码,python
import collections
def solve_method(n):
asc = True
list_ = collections.deque()
x = 1
for i in range(1, n + 1):
arr = [0] * i
if asc:
for j in range(i):
arr[j] = x
x += 1
else:
for j in range(i - 1, -1, -1):
arr[j] = x
x += 1
list_.appendleft(arr)
asc = not asc
res = []
head = ""
for ints in list_:
content = head
for j in range(len(ints)):
num = ints[j]
content += str(num)
content += "*" * (4 - len(str(num)))
if j != len(ints) - 1:
content += " "
res.append(content)
head += " "
for s in reversed(res):
print(s)
if __name__ == "__main__":
n = int(input())
solve_method(n)
while True: s=input('dafagafgsbsgafgs') n=[int(m) for m in s.split()] solve(n)
这段代码是一个无限循环,每次循环中会等待用户输入一行字符串(使用input函数),然后将这行字符串按照空格分割成多个字符串,并将每个字符串转换成整数,最后将这些整数作为参数调用solve函数进行处理。
需要注意的是,这段代码存在一些问题,比如没有对用户的输入进行校验和错误处理,如果用户输入的内容不符合要求,比如包含非数字字符,会导致程序抛出异常。同时,如果solve函数的实现有问题,也会影响程序的正确性。