-
Notifications
You must be signed in to change notification settings - Fork 0
/
anagram.cpp
58 lines (40 loc) · 1.06 KB
/
anagram.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <set>
#include <algorithm>
#define MAXN 10000
using namespace std;
char text[MAXN+1];
multiset <char> s1, s2;
int t, len, mid;
int main( ) {
scanf( "%d", &t );
while ( t-- ) {
scanf( "%s", text );
len = strlen( text );
if ( len & 1 ) {
printf( "-1\n" );
continue;
}
mid = len / 2;
int cnt1 = 0;
int cnt2 = 0;
s1.clear();
s2.clear();
for ( int i = 0; i < mid; ++i ) {
s1.insert( text[i] );
s2.insert( text[len-i-1] );
}
for ( multiset<char>::iterator pos = s1.begin(); pos != s1.end(); ++pos ) {
if ( s2.find( *pos ) != s2.end() ) {
multiset<char>::iterator tmp = s2.find( *pos );
s2.erase( tmp );
}
}
printf( "%d\n", s2.size() );
}
return 0;
}