time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 s�, 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 s�.
Input
The first line of input contains an integer t� (1≤t≤1041≤�≤104) — the number of test cases.
Following that are the descriptions of the test cases.
The first line of each test case contains an integer n� (2≤n≤502≤�≤50) — the length of the melody s�.
The second line of each test case contains a string s� of length n�, consisting of characters 'a', 'b', 'c', 'd', 'e', 'f', 'g'.
Output
Output t� 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.
Example
input
Copy
5
4
abab
7
abacaba
6
aaaaaa
7
abcdefg
5
babdd
output
Copy
2 4 1 6 4
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".文章来源:https://www.toymoban.com/news/detail-471251.html
解题说明:此题是一道字符串题目,两个字母为一组,只需要遍历整个字符串,以组为单位统计出不同的个数即可。文章来源地址https://www.toymoban.com/news/detail-471251.html
#include<stdio.h>
#include<string.h>
int main()
{
int n;
scanf("%d", &n);
while (n--)
{
int m, i, j, cnt = 0;
char s[60];
scanf("%d", &m);
scanf("%s", s);
for (i = 1; i < strlen(s) - 1; i++)
{
for (j = 0; j < i; j++)
{
if (s[i] == s[j] && s[i + 1] == s[j + 1])
{
break;
}
}
if (j == i)
{
cnt++;
}
}
printf("%d\n", cnt + 1);
}
return 0;
}
到了这里,关于A. Musical Puzzle的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!