C. Diluc and Kaeya
原题链接
https://codeforces.com/problemset/problem/1536/Chttps://codeforces.com/problemset/problem/1536/C
Example
input
Copy
5 3 DDK 6 DDDDDD 4 DKDK 1 D 9 DKDKDDDDK
output
Copy
1 2 1 1 2 3 4 5 6 1 1 1 2 1 1 1 1 2 1 2 1 1 3
大意就是一串字符串,只有D和K,从开头数到结尾的前缀能够分成几份比值相同的子字符串,如
DKDKDDDDK输出第一个肯定是1,当输出到第四个数时,此时是求前缀DKDK能够分成几份比值相同的部分,那肯定就是2份。
热知识:当D和K的数量互质时只能分成一份。
#include <iostream>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
typedef pair<int, int> pii;//将比值用pair打包
const int manx = 2e6 + 10;
int a[manx];
int b[manx];
int gcd(int x, int y)//最大公约数因为求最多份。
{
int r;
while (y>0)
{
r = x % y;
x = y;
y = r;
}
return x;
}//辗转相除法
int main()
{
int t;
cin >> t;
while (t--)
{
int t, n, g, d, k;
string s;
d = 0, k = 0;
map<pair<int, int>,int> mp;//将比值和比值出现的次数一一映射
cin >> n >> s;
for (int i = 0; i < n; i++)
{
if (s[i] == 'D')
d++;
else
k++;
g = gcd(d, k);
mp[pii(d / g, k / g)]++;//比值出现的次数加一
cout << mp[pii(d / g, k / g)] << " ";//直接输出map数据结构,只会输出最右边的值,如map<pair<int, int>,int>只会输出最后的int 值
//即只输出比值出现的次数,也就是分成的份数。
}
cout << endl;
}
}
对map的理解有着很强的加深作用。