POJ 3087 Shuffle'm Up

这题应该不是搜索,勉强算个hash把。其实是个水模拟。。。

判断-1的方法就是,看是否是得到了以前出现过的字符串,所以用set或者map都可以。

这里用的是set。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
using namespace std;
set<string> st;
int n;
char *shuffle(char *str2,char *str1)
{
    int i,j;
    char str[3000];
    i=0;
    j=0;
    while (i != n)
    {
        str[j]=str1[i];
        str[j+1]=str2[i];
        j+=2;
        i++;
    }
    str[j]='\0';
    return str;
}
int main()
{
    int T,i,j,cnt,prob;
    char str1[2000],str2[2000],str3[3000],ts[3000];
    scanf("%d",&T);
    prob=1;
    while (T--)
    {
        scanf("%d",&n);
        getchar();
        scanf("%s%s%s",str1,str2,str3);
        strcpy(ts,shuffle(str1,str2));
        st.clear();
        st.insert(ts);
        cnt=1;
        printf("%d ",prob);
        prob++;
        if (strcmp(ts,str3) == 0)
        {
            printf("%d\n",cnt);
            continue;
        }
        while (1)
        {
            strcpy(ts,shuffle(ts,ts+n));

            cnt++;
            if (strcmp(ts,str3) == 0)
            {
                printf("%d\n",cnt);
                break;
            }
            if (st.find(ts) != st.end())
            {
                printf("-1\n");
                break;
            }
            st.insert(ts);
        }
    }
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值