J - Just a Magic String

J - Just a Magic String

Time Limit: 1000/1000MS (Java/Others)     Memory Limit: 131072/131072KB (Java/Others)

Description

You have such a string $S$ a, every time you can copy $S$ to $T$ ,change a in $T$ to bb to a, then add $T$ after $S$.

For example,

a

ab

abba

abbabaab

abbabaabbaababba

......

Finally you will get a infinite magic string.

Now, given a string $X$ only containing a and b, you should tell me if it appears in the magic string?

If it appears, than output the location it first appears, otherwise, output -1;

Input

A line with a string that consists only a and b and no more than $10^6$ characters.

Output

Print the position of the first occurrence of the string, or -1 if it doesn‘t exist.

Sample input

bab

Sample output

3

Sample input

baab

Sample output

5

Sample input

aaabbb

Sample output

-1

总结

想了半天不知道怎么做,结果还是楚枫说的kmp算法来解决的

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int kmp_find(const string& target,const string& pattern)
{
    const int target_length = target.size();
    const int pattern_length = pattern.size();
    int * overlay_value = new int[pattern_length];
    overlay_value[0] = -1;
    int index = 0;
    for(int i=1;i<pattern_length;++i)
    {
        index = overlay_value[i-1];
        while(index>=0 && pattern[index+1]!=pattern[i])
        {
            index  = overlay_value[index];
        }
        if(pattern[index+1]==pattern[i])
        {
            overlay_value[i] = index +1;
        }
        else
        {
            overlay_value[i] = -1;
        }
    }
    //match algorithm start
    int pattern_index = 0;
    int target_index = 0;
    while(pattern_index<pattern_length&&target_index<target_length)
    {
        if(target[target_index]==pattern[pattern_index])
        {
            ++target_index;
            ++pattern_index;
        }
        else if(pattern_index==0)
        {
            ++target_index;
        }
        else
        {
            pattern_index = overlay_value[pattern_index-1]+1;
        }
    }
    if(pattern_index==pattern_length)
    {
        return target_index-pattern_index;
    }
    else
    {
        return -2;
    }
    delete [] overlay_value;
}
int main()
{
    string source="a";
    string a=source;
    string pattern;
    int n=25;

    cin >>pattern;

    for(int i=1;i<=n;i++){
        a=source;
        for(int j=0;j<a.length();j++){
            if(a[j]==‘a‘) a[j]=‘b‘;
            else a[j]=‘a‘;
        }
        source+=a;
    }
    cout<<kmp_find(source,pattern)+1<<endl;
    return 0;
}
时间: 2024-08-26 14:00:39

J - Just a Magic String的相关文章

WPF系列之三:实现类型安全的INotifyPropertyChanged接口,可以不用“Magic string” 么?

通常实现INotifyPropertyChanged接口很简单,为你的类只实现一个PropertyChanged 的Event就可以了. 例如实现一个简单的ViewModel1类: public class ViewModel1 : INotifyPropertyChanged { private string _data; public string Data { get { return _data; } set { if (_data == value) return; _data = v

HDU 5157 Harry and magic string(回文树)

Harry and magic string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 223    Accepted Submission(s): 110 Problem Description Harry got a string T, he wanted to know the number of T's disjoint

HDU-5157Harry and magic string

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5157 先从后往前插点,在构造回文树时,让cnt[i]+=cnt[fail[i]],然后维护一个后缀和a. 再从前往后插点,每个点对答案的贡献为cnt[i]*a[i+1] #include<cstring> #include<iostream> #include<algorithm> #include<cstdio> #define rep(i,l,r) for (i

HDU - 5157 :Harry and magic string (回文树)

Sample Input aca aaaa Sample Output 3 15 题意: 多组输入,每次给定字符串S(|S|<1e5),求多少对不相交的回文串. 思路:可以用回文树求出以每个位置结尾的回文串数,那么累加得到前缀和: 倒着再做一遍得到每个位置为开头的回文串数,乘正向求出的前缀和即可. #include<bits/stdc++.h> #define ll long long #define rep(i,a,b) for(int i=a;i<=b;i++) #define

hdu 5030 Rabbit&#39;s String(后缀数组&amp;二分)

Rabbit's String Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 288    Accepted Submission(s): 108 Problem Description Long long ago, there lived a lot of rabbits in the forest. One day, the

Java 连接String的几种方式

public class StringTest implements Clock {     private int i = 0;     public void testString() {         String str = new String();         int j = i;         for(; i < j + 40000; i++) {             str += String.valueOf(i);         }     }     publi

HDU5008 Boring String Problem(后缀数组 + 二分 + 线段树)

题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5008 Description In this problem, you are given a string s and q queries. For each query, you should answer that when all distinct substrings of string s were sorted lexicographically, which one is

string类例题

//string a = "aldsfdh"; ////bool q = a.Contains("dd");//是否包含此字符串 //int o = a.IndexOf("h"); //Console.WriteLine(o); ////int p = a.LastIndexOf("d"); ////Console.WriteLine(p); //int b = a.Length;//长度 //Console.WriteLin

JAVA学习第三十课(经常使用对象API)- String类:类方法练习

intern方法 public class Main { public static void main(String[] args) { String str1 = new String("asd"); String str2 = str1.intern();/* 字符串常量池中有,就返回字符串,没有就创建 */ System.out.println(str2); System.out.println( str1 == str2 ); } } 练习1:字符串数组排序 import j