Submission #1226986


Source Code Expand

#include <queue>
#include <map>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

#define TRACE(x) cerr << #x << " = " << x << endl
#define REP(i, n) for (int i=0; i<n; i++)
#define FOR(i, a, b) for (int i=(a); i<(b); i++)
#define _ << " " <<

typedef long long ll;
typedef pair<int, int> P;
#define X first
#define Y second

const int MAX = 1<<20, ALFA = 26;

char poc[MAX], kraj[MAX];
int pozl[MAX][ALFA];
int pref[MAX];
int n;

map <string, int> M;
queue <string> Q;

void rek(const string &pat, string s, int poz, int val) {
  if (poz == n) {
    if (M.find(s) == M.end()) {
      M[s] = val;
      Q.push(s);      
    }    
    return;
  }

  s[poz] = pat[poz];
  rek(pat, s, poz+1, val);

  if (poz) {
    s[poz] = s[poz-1];
    rek(pat, s, poz+1, val);
  }
}

int brute() {
  M.clear();
  for (; !Q.empty(); Q.pop());

  string pc = poc, kr = kraj;
  M[pc] = 0;

  TRACE(pc _ kr);

  Q.push(pc);

  for (; !Q.empty(); ) {
    string tmp = Q.front(); Q.pop();
    if (tmp == kr) return M[tmp];
    rek(tmp, tmp, 0, M[tmp]+1);
  }
  return -1;
}

void gen() {
  n = rand() % 6 + 1;
  REP(i, n) poc[i] = (char) (rand() % 2 + 'a');
  REP(i, n) kraj[i] = (char) (rand() % 2 + 'a');
  poc[n] = kraj[n] = 0;

  // n = 4;
  // poc[0] = 'a'; poc[1] = 'b'; poc[2] = 'a'; poc[3] = 'a'; poc[4] = 0;
  // kraj[0] = 'a'; kraj[1] = 'a'; kraj[2] = 'b'; kraj[3] = 'b'; kraj[4] = 0;
}

const int TOUR = 1<<20;
int t[2*TOUR];

void stavi(int pos, int val) {
  pos += TOUR;
  for (; pos; pos /= 2)
    t[pos] = max(t[pos], val);
}

int vrati(int pos, int lo, int hi, int begin, int end) {
  if (lo >= end || hi <= begin) return 0;
  if (lo >= begin && hi <= end) return t[pos];
  return max(vrati(2*pos+0, lo, (lo+hi)/2, begin, end),
	     vrati(2*pos+1, (lo+hi)/2, hi, begin, end));
}

int solve() {
  memset(t, 0, sizeof t);
  memset(pozl, -1, sizeof pozl);

  REP(i, n) {
    if (i)
      memcpy(pozl[i], pozl[i-1], sizeof pozl[i]);
    pozl[i][poc[i]-'a'] = i;
  }
  
  int poz = n-1, rje = 0;
  for (int i=n-1; i>=0; i--) {
    poz = min(poz, i);

    int tmp = pozl[poz][kraj[i]-'a'];
    if (tmp == -1) return -1;
  
    int val = vrati(1, 0, TOUR, tmp+1, i+1) + (tmp != i);
    rje = max(rje, val);
    stavi(tmp, val);
    rje = max(rje, val);

    poz = tmp;
  }  

  return rje;
}

int main()
{
  scanf("%d %s %s", &n, poc, kraj);
  printf("%d\n", solve());
  //   TRACE(solve() _ brute());
  return 0;
  
  for (;;) {
    gen();
    TRACE(n _ poc _ kraj);
    int sl = solve(), br = brute();
    TRACE(sl _ br);
    assert(sl == br);
  }

  return 0;
}

Submission Info

Submission Time
Task F - Shik and Copying String
User dbradac
Language C++14 (GCC 5.4.1)
Score 0
Code Size 2781 Byte
Status WA
Exec Time 405 ms
Memory 119168 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:126:35: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %s %s", &n, poc, kraj);
                                   ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 1500
Status
AC × 4
AC × 36
WA × 27
Set Name Test Cases
Sample example0.txt, example1.txt, example2.txt, example3.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, 042.txt, 043.txt, 044.txt, 045.txt, 046.txt, 047.txt, 048.txt, 049.txt, 050.txt, 051.txt, 052.txt, 053.txt, 054.txt, 055.txt, 056.txt, 057.txt, 058.txt, example0.txt, example1.txt, example2.txt, example3.txt
Case Name Status Exec Time Memory
000.txt AC 30 ms 119040 KB
001.txt AC 30 ms 119040 KB
002.txt AC 30 ms 119040 KB
003.txt AC 30 ms 119040 KB
004.txt AC 30 ms 119040 KB
005.txt AC 30 ms 119040 KB
006.txt AC 30 ms 119040 KB
007.txt AC 30 ms 119040 KB
008.txt AC 30 ms 119040 KB
009.txt AC 30 ms 119040 KB
010.txt WA 30 ms 119040 KB
011.txt WA 30 ms 119040 KB
012.txt WA 30 ms 119040 KB
013.txt WA 30 ms 119040 KB
014.txt WA 30 ms 119040 KB
015.txt WA 30 ms 119040 KB
016.txt WA 30 ms 119040 KB
017.txt WA 30 ms 119040 KB
018.txt WA 30 ms 119040 KB
019.txt WA 30 ms 119040 KB
020.txt WA 30 ms 119040 KB
021.txt WA 30 ms 119040 KB
022.txt WA 30 ms 119040 KB
023.txt WA 30 ms 119040 KB
024.txt WA 30 ms 119040 KB
025.txt AC 247 ms 119040 KB
026.txt AC 264 ms 119040 KB
027.txt AC 405 ms 119040 KB
028.txt WA 280 ms 119040 KB
029.txt WA 295 ms 119040 KB
030.txt WA 248 ms 119040 KB
031.txt WA 250 ms 119040 KB
032.txt WA 255 ms 119040 KB
033.txt WA 258 ms 119040 KB
034.txt WA 267 ms 119040 KB
035.txt WA 270 ms 119040 KB
036.txt WA 280 ms 119040 KB
037.txt WA 286 ms 119040 KB
038.txt WA 294 ms 119040 KB
039.txt WA 305 ms 119040 KB
040.txt AC 283 ms 119040 KB
041.txt AC 280 ms 119040 KB
042.txt AC 275 ms 119040 KB
043.txt AC 274 ms 119040 KB
044.txt AC 273 ms 119040 KB
045.txt AC 273 ms 119040 KB
046.txt AC 273 ms 119040 KB
047.txt AC 273 ms 119040 KB
048.txt AC 273 ms 119040 KB
049.txt AC 273 ms 119040 KB
050.txt AC 263 ms 119040 KB
051.txt AC 289 ms 119040 KB
052.txt AC 233 ms 119040 KB
053.txt AC 337 ms 119040 KB
054.txt AC 344 ms 119040 KB
055.txt AC 360 ms 119040 KB
056.txt AC 367 ms 119040 KB
057.txt AC 385 ms 119168 KB
058.txt AC 394 ms 119040 KB
example0.txt AC 30 ms 119040 KB
example1.txt AC 30 ms 119040 KB
example2.txt AC 30 ms 119040 KB
example3.txt AC 30 ms 119040 KB