一组字符串给出两种排列方式,
求最小分成多少组 如
A A
B C
C D
D B
E E
则分成3组
A
B C D
E
即为1 3 1
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <queue> #include <algorithm> #include <string> #include <map> using namespace std; map <string, int> maps; int main() { int T; cin >> T; while(T--) { int n; //char s[11]; cin >> n; maps.clear(); string s; for(int i = 0; i < n; i++) { cin >> s; //maps.insert(pair<string, int>(s, i)); maps[s] = i;//map把一个s和i对应起来 } int ans = 0, idx = 0; for(int i = 0; i < n; i++) { cin >> s; /*map<string, int>::iterator iter; iter = maps.find(s); int t = iter -> second;*/ int t = maps[s]; idx = max(idx, t); ans++; if(idx == i) printf("%d ", ans), ans = 0; } printf(" "); } return 0; }