zoukankan      html  css  js  c++  java
  • 1423. String Tale

    1423. String Tale

    Time limit: 1.0 second Memory limit: 64 MB

    Background

    I want to tell you a story. Not entirely, but only the very beginning, because the ending of this story became a legend of programming—as well as its heroes.
    When computers were large, when trees were small, when the sun shined brighter… Once upon a time there were Three Programmers. I doubt whether they participated in any programming contests, because there were no contests at that ancient time. There was neither ACM ICPC nor Timus Online Judge. But there were Three Programmers.

    Problem

    One day Three Programmers invented an amusing game to train memory and mental faculties. The First Programmer thought out a string S which was N characters long and passed it to the Second and the Third Programmers. The Second Programmer executed X (0 ≤ X < N) successive cycle shifts (a cycle shift is a transfer of the last character of the string to the beginning of this string) with this string. As a result of these operations a string T was produced, and the Second Programmer passed it to the Third Programmer. A task of the Third Programmer was to find the number X or make sure that the Second Programmer was mistaken, because the string T could not be produced from the string S via cycle shifts.

    Input

    The first line contains the integer number N (1 ≤ N ≤ 250000). The second line contains the string S. The third line contains the string T. Each string has length N and may contain any ASCII characters with codes from 33 to 255.

    Output

    If the string T can be produced from the string S via cycle shifts you should output the desired number X, otherwise you should output “−1”. If the problem has several solutions, you may output any of them.

    Sample

    inputoutput
    11
    abracadabra
    racadabraab
    9
    

    Hint

    Let us consider the strings S = “abracadabra” and T = “racadabraab”. The string T can be produced from the string S via 9 cycle shifts: “abracadabra” > “aabracadabr” > “raabracadab” > “braabracada” > “abraabracad” > “dabraabraca” > “adabraabrac” > “cadabraabra” > “acadabraabr” > “racadabraab”
    Problem Author: Nikita Rybak, Ilya Grebnov, Dmitry Kovalioff Problem Source: Timus Top Coders: First Challenge
    ***************************************************************************************
    大坑题,,忘啦考虑不用动时的情况啦!!!kmp算法
    ***************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 #include<cstdio>
     7 #include<queue>
     8 #include<vector>
     9 #include<stack>
    10 using namespace std;
    11 string pat,text;
    12 int next[250010];
    13 int lenp,lent;
    14 int n;
    15 void  getnext()//next数组
    16  {
    17      int h=-1,k=0;
    18      next[0]=-1;
    19      while(k<lenp)
    20       {
    21           if(h==-1||pat[h]==pat[k])
    22            {
    23                h++;k++;
    24                next[k]=h;
    25            }
    26            else
    27              h=next[h];
    28       }
    29  }
    30  int kmp()//kmp
    31  {
    32      int i=0,j=0;
    33      getnext();
    34      while(j<lenp&&i<lent)
    35       {
    36           if(j==-1||pat[j]==text[i])
    37            {
    38                i++;j++;
    39 
    40            }
    41            else
    42              j=next[j];
    43         if(j==lenp)
    44         return lent-i;
    45        }
    46 
    47       return -1;
    48  }
    49  int main()
    50  {
    51      cin>>n;
    52      int i;
    53      memset(next,-1,sizeof(next));
    54      cin>>text;
    55      cin>>pat;
    56      text+=text;
    57      lent=text.length();
    58      lenp=pat.length();
    59       if(kmp()==n)//zhuyi!!!!!!!!!!
    60        cout<<'0'<<endl;
    61       else
    62        cout<<kmp()<<endl;
    63             return 0;
    64  }
    View Code
  • 相关阅读:
    map 取最大value
    tmux 常用快捷键
    史蒂夫 乔布斯(Steve Jobs)在 Stanford 2005年毕业典礼上的演讲(中英文对照)
    黄聪:WordPress后台添加侧边栏菜单(WP教程add_menu_page)
    黄聪:ASP.NET AJAX入门系列(1) AjaxControlToolkit安装篇
    黄聪:ASP.NET AJAX入门系列(2) Accordion控件
    黄聪:wordpress调用函数大全
    黄聪:使用Wordpress中的wpdb类操作数据库
    黄聪:C#的Microsoft图表控件
    黄聪:如何写出兼容性强的CSS代码,适应各种浏览器:IE、火狐等
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3274902.html
Copyright © 2011-2022 走看看