AtCoder Grand Contest 007 : F - Shik and Copying String


题目链接:   https://agc007.contest.atcoder.jp/tasks/agc007_f

F - Shik and Copying String


Time limit : 2sec / Memory limit : 256MB

Score : 1500 points

Problem Statement

Shik's job is very boring. At day 0, his boss gives him a string S0 of length N which consists of only lowercase English letters. In the i-th day after day 0, Shik's job is to copy the string Si1 to a string Si. We denote the j-th letter of Si as Si[j].

Shik is inexperienced in this job. In each day, when he is copying letters one by one from the first letter to the last letter, he would make mistakes. That is, he sometimes accidentally writes down the same letter that he wrote previously instead of the correct one. More specifically, Si[j] is equal to either Si1[j] or Si[j1]. (Note that Si[1] always equals to Si1[1].)

You are given the string S0 and another string T. Please determine the smallest integer i such that Si could be equal to T. If no such i exists, please print -1.

Constraints

  • 1N1,000,000
  • The lengths of S0 and T are both N.
  • Both S0 and T consist of lowercase English letters.

Input

The input is given from Standard Input in the following format:

N
S0
T

Output

Print the smallest integer i such that Si could be equal to T. If no such i exists, print -1 instead.


Sample Input 1

Copy
5
abcde
aaacc

Sample Output 1

Copy
2

S0 = abcdeS1 = aaccc and S2 = aaacc is a possible sequence such that S2=T.


Sample Input 2

Copy
5
abcde
abcde

Sample Output 2

Copy
0

Sample Input 3

Copy
4
acaa
aaca

Sample Output 3

Copy
2

Sample Input 4

Copy
5
abcde
bbbbb

Sample Output 4

Copy
-1


题意:

    S->T 变化,经过几次得到

   字母改变的方式有两种:     S[i][j] = S[i-1][j] || S[i][j-1]  


思路:

    类似于 模拟的操作,  去找 变化不同的, 然后 可以用一个数组来记录 变化的次数,    可以写一写 感受一下


code:

#include 
#include 

typedef long long ll;
const int MAXN=1e6+7;

using namespace std;
int c[MAXN+5];
int main()
{
    string s,t;
    int n;
    cin>>n;
    cin>>s>>t;
    if(s[0]!=t[0]|| s.length()!=t.length()){return 0*puts("-1");}
    int ans=0,k=0,sum=0;
    for(int i=n-1,j=n-1;i>=0;i--)
    {
        sum+= c[i+k];
        if( j>i || t[i]!=s[j])
        {
            while( j>=0 &&( j>i|| t[i]!=s[j])) j--;
            if(j==i) continue;
            if(j<0)
            {
                return 0*puts("-1");
            }
            k++,sum++;
            c[i+k-1]=0,c[j+k-1]--;
        }
        ans=max(ans,sum);
    }
    cout<

123 

转载于:https://www.cnblogs.com/sizaif/p/9078357.html

你可能感兴趣的:(AtCoder Grand Contest 007 : F - Shik and Copying String)