欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1833A.Musical Puzzle
Musical Puzzle
Vlad decided to compose a melody on his guitar. Let's represent the melody as a sequence of notes corresponding to the characters 'a', 'b', 'c', 'd', 'e', 'f', and 'g'.
However, Vlad is not very experienced in playing the guitar and can only record exactly two notes at a time. Vlad wants to obtain the melody , and to do this, he can merge the recorded melodies together. In this case, the last sound of the first melody must match the first sound of the second melody.
For example, if Vlad recorded the melodies "ab" and "ba", he can merge them together and obtain the melody "aba", and then merge the result with "ab" to get "abab".
Help Vlad determine the minimum number of melodies consisting of two notes that he needs to record in order to obtain the melody .
Input
The first line of input contains an integer () — the number of test cases.
Following that are the descriptions of the test cases.
The first line of each test case contains an integer () — the length of the melody .
The second line of each test case contains a string of length , consisting of characters 'a', 'b', 'c', 'd', 'e', 'f', 'g'.
Output
Output integers, each representing the answer for the corresponding test case. As the answer output minimum number of melodies consisting of two notes that Vlad needs to record.
Note
In the first sample, you need to record the melodies "ab" and "ba", as described in the problem statement.
In the second sample, you need to record the melodies "ab", "ba", "ac", and "ca".
In the third sample, the only necessary melody is "aa".
Samples
5
4
abab
7
abacaba
6
aaaaaa
7
abcdefg
5
babdd
2
4
1
6
4
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |