zoukankan      html  css  js  c++  java
  • 字符串循环移位包含

    题目:

          给定两个字符串S1,S2,要求判断是否通过S1循环移位可以得到S2,比如说:S1 = AABCD,S2 = CDAA,结果返回true。给定S1=ABCD和S2=ACBD,结果返回false。

    方法1:

          最简单的就是将S1循环一遍,每一次都是将S1循环右移一位,然后和S2进行比较。效率低到O(len(S1)*len(S2)).

    代码:

    #include "stdafx.h"
    #include<iostream>
    #include<stdio.h>
    #include<time.h>
    #include<math.h>
    #include<stdlib.h>
    #include<algorithm>
    #include<queue>
    #include<Windows.h>
    using namespace std;
    //O(len(s1)*len(s2))
    #define MAX 100
    char s1[MAX];
    char s2[MAX];
    //测试是否符合题意
    bool test()
    {
     int len1=strlen(s1);
     int len2=strlen(s2);
     //每次将s1循环右移一位
     for(int i=0;i<len1;i++)
     {
      char t=s1[len1-1];
      //循环右移的话从尾向前比较方便,循环左移的话从头到尾比较方便
      for(int i=len1-2;i>=0;i--)
      {
       s1[i+1]=s1[i];
      }
      s1[0]=t;
      //进行比较
      if(strstr(s1,s2)!=NULL)
      {
       return true;
      }
     }
     return false;
    }
    int _tmain(int argc, _TCHAR* argv[])
    {
     while(cin>>s1>>s2)
     {
      if(test())
      {
       cout<<"true"<<endl;
      }
      else
      {
       cout<<"false"<<endl;
      }
     }

     ::system("pause");
     return 0;
    }

    方法2:

         以S1=ABCD为例,分析s1的循环移位得到的结果,如下所示:

        ABCD->BCDA->CDAB->DABC->ABCD->.....

         假设我们把前面移走的数据进行保留,会发现有如下规律:
         ABCD-> ABCDA-> ABCDAB->ABCDABC->ABCDABCD


       这里,我们可以发现,实际对s1的循环移位得到的字符串实际为s1s1。只需要判断s1s1中是否含有s2即可

    代码:

    #include "stdafx.h"
    #include<iostream>
    #include<stdio.h>
    #include<time.h>
    #include<math.h>
    #include<stdlib.h>
    #include<algorithm>
    #include<queue>
    #include<Windows.h>
    using namespace std;
    #define MAX 100
    char s1[MAX];
    char s2[MAX];
    char s3[MAX*2];
    //测试是否符合题意
    bool test()
    {
     int len1=strlen(s1);
     int len2=strlen(s2);
     strcpy(s3,s1);
     strcpy(s3+len1,s1);
     if(strstr(s3,s2)!=NULL)
     {
      return true;
     }
     return false;
    }
    int _tmain(int argc, _TCHAR* argv[])
    {
     while(cin>>s1>>s2)
     {
      if(test())
      {
       cout<<"true"<<endl;
      }
      else
      {
       cout<<"false"<<endl;
      }
     }

     ::system("pause");
     return 0;
    }

  • 相关阅读:
    大数加法、乘法实现的简单版本
    hdu 4027 Can you answer these queries?
    zoj 1610 Count the Colors
    2018 徐州赛区网赛 G. Trace
    1495 中国好区间 尺取法
    LA 3938 动态最大连续区间 线段树
    51nod 1275 连续子段的差异
    caioj 1172 poj 2823 单调队列过渡题
    数据结构和算法题
    一个通用分页类
  • 原文地址:https://www.cnblogs.com/fickleness/p/3155038.html
Copyright © 2011-2022 走看看