Test instructions: Give a permutation to determine if it is the square of another permutation.
A permutation can be expressed as a product of a loop. For even-numbered loops, the square of it splits into two lengths of the previous half of the cycle. For a loop with an odd length, its square is not split. So for the current permutation in the length of the even-numbered loops, must be the previous displacement split out, to determine whether each length of the number of even-numbered loops is an even number. For loops with an odd length, do not consider.
Code:
#include <cstdlib> #include <cctype> #include <cstring> #include <cstdio> #include <cmath > #include <climits> #include <algorithm> #include <vector> #include <string> #include < iostream> #include <sstream> #include <map> #include <set> #include <queue> #include < stack> #include <fstream> #include <numeric> #include <iomanip> #include <bitset> #include <list> #include <stdexcept> #include <functional> #include <utility> #include <ctime> Using namespace std, #define PB push_back#define MP make_pair#define REP (i,x,n) for (int i=x;i< (n); ++i) #define for (I,l, h) for (int i= (l); i<= (h), ++i) #define FORD (i,h,l) for (int i= (h); i>= (l); i) #define SZ (x) ((int) (x). Size ()) #define All (x) (x). Begin (), (x). End () #define RI (x) scanf ("%d", & (x)) #define RII (x, Y) scanf ("%d%d", & (X), & (Y)) # Define RIII (x, Y, z) scanf ("%d%d%d", & (X), & (Y), & (z)) #define DRI (x) int (x); scanf ("%d", &x) #define DRII (x, y) int X, y; scanf ("%d%d", &x, &y) #define DRIII (x, y, z) int x, y, Z; scanf ("%d%d%d", &x, &y, &z) #define OI (x) printf ("%d", x), #define RS (x) scanf ("%s", (x)) #define MS0 (x) memset ( (x), 0, sizeof ((x))) #define MS1 (x) memset ((x),-1, sizeof ((x))) #define LEN (x) strlen (x) #define F first#define S second#def Ine Swap (A, B) (a ^= B, b ^= A, a ^= b) #define Dpoint strcut node{int x, y} #define CMPD int cmp (const int &A,CONST int &B) {return a>b;}/* #ifdef HOME freopen ("In.txt", "R", stdin); #endif */const int MOD = 1e9+7;typedef vector<int> vi;typedef vector<string> vs;typedef vector<double> Vd;typedef Long Long ll;typedef pair<int,int> pii;//#define Homeint Scan () {int res = 0, ch, flag = 0;if (ch = getcha R ()) = = '-')//determine positive and negative flag = 1;else if (ch >= ' 0 ' && ch <= ' 9 ')//Get complete number res = CH-' 0 '; while ((ch = getchar ()) > = ' 0 ' && ch <= ' 9 ') res = res * + CH-' 0 '; return flag? -res:res;}/*----------------Please-----does-----not-----HACK-----ME--------------------*/char s[30];int a[30];int cnt[30]; int Vis[30];int Main () {int n; RI (n); while (n--) {scanf ("%s", s); REP (i,0,26) {a[i]=s[i]-' a '; } MS0 (CNT); MS0 (VIS); REP (i,0,26) {int len=0; if (!vis[a[i]]) {len++; Vis[a[i]]=true; int Now=a[a[i]]; while (!vis[now]) {len++; Vis[now]=true; Now=a[now]; }} cnt[len]++; } int ok=1; for (int i=2;i<27;i+=2) {if (cnt[i]%2!=0) {ok=0; Break }} if (OK) printf ("yes\n"); else printf ("no\n");} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Substitution Radical POJ3128