poj 3128 Leonardo's Notebook——思路(置换)

题目:http://poj.org/problem?id=3128

从环的角度考虑。

原来有奇数个点的环,现在点数不变;

原来有偶数个点的环(设有 k 个点),现在变成两个大小为 k/2 的环。

所以判断一下现在的有偶数个点的环是不是成双成对的就行了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=30;
int n,cnt[N];
bool vis[N],flag; char ch[N];
int main()
{
  int T;scanf("%d",&T);
  while(T--)
    {
      scanf("%s",ch+1);
      memset(vis,0,sizeof vis);
      memset(cnt,0,sizeof cnt);
      for(int i=1;i<=26;i++)
    if(!vis[i])
      {
        int tot=0,cr=i;
        while(!vis[cr])
          {
            tot++; vis[cr]=1;
        cr=ch[cr]-'A'+1;
          }
        if((tot&1)==0)cnt[tot]++;
      }
      flag=0;
      for(int i=2;i<=26;i++)if(cnt[i]&1){flag=1;break;}
      puts(flag?"No":"Yes");
    }
  return 0;
}

 

posted on 2018-12-03 20:59  Narh  阅读(190)  评论(0)    收藏  举报

导航