Editorial for Gửi thư


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

Code mẫu của flashmt

var a,b:string;
    la,lb,i,j,re:integer;
    kt:boolean;
begin
     readln(a);
     read(b);

     la:=length(a); lb:=length(b); re:=la+1;
     for i:=la downto 1 do
     begin
          kt:=false;
          for j:=0 to la-i do
              if a[i+j]<>b[j+1] then
              begin
                   kt:=true;
                   break;
              end;
          if not kt then re:=i;
     end;
     write(lb-1+re);
end.

Code mẫu của happyboy99x

#include<cstdio>
#include<cstring>

#define N 255 
#define MOD 1000000007LL
typedef long long LL;

char a[N], b[N];
int n, m;
LL hashA[N], pow[N], hashB[N];

void init() {
    hashA[0] = 0; hashB[0] = 0; pow[0] = 1;
    for(int i = 1; i <= n; ++i) hashA[i] = (hashA[i-1] * 26 + a[i] - 'a') % MOD;
    for(int i = 1; i <= m; ++i) hashB[i] = (hashB[i-1] * 26 + b[i] - 'a') % MOD;
    for(int i = 1, _n = n > m ? n : m; i <= _n; ++i) pow[i] = (pow[i-1] * 26) % MOD;
}

inline LL getHashA(int i, int j) {
    return (hashA[j] - hashA[i-1] * pow[j-i+1] + MOD * MOD) % MOD;
}

inline LL getHashB(int i, int j) {
    return (hashB[j] - hashB[i-1] * pow[j-i+1] + MOD * MOD) % MOD;
}

int main() {
    scanf("%s%s", a+1, b+1); n = strlen(a+1); m = strlen(b+1);
    init();
    for(int i = n>=m ? n-m+1 : 1, j = n>=m ? m : n; i<=n&&j>0; ++i, --j) {
        if(getHashA(i, n) == getHashB(1, j)) {
            printf("%d\n", n + m - j);
            return 0;
        }
    }
    printf("%d\n", n+m);
    return 0;
}

Code mẫu của ladpro98

const fi='';
      fo='';
var sb, se : string;
    f:text;
procedure docf;
begin
        assign(f,fi);
        reset(f);
        readln(f,sb);
        readln(f,se);
        close(f);
end;
procedure lam;
var st : string;
begin
        st := sb;
        while (pos(st, se) <> 1) and (length(st) > 0) do delete(st, 1, 1);
        delete(se, 1, length(st));
        writeln(length(sb) + length(se));
end;
procedure ghif;
begin
        assign(f,fO);
        rewrite(f);
end;
begin
            docf;
            ghif;
            lam;
            close(f);
end.

Code mẫu của hieult

#include <stdio.h>
#include <string.h>
main()
{
char s1[251],s2[251];
int n1,n2,m,t;
scanf("%s %s",s1,s2);
n1=strlen(s1);
m=n1;
n2=strlen(s2);
for(int i=0;i<n1;i++)
  {
  t=0;
  for(int j=i;j<n1;j++)
    if(s1[j]!=s2[j-i])
      {
      t=1;
      break;
      }
  if(t==0)
    {
    m=i;
    break;
    }
  }
printf("%d",m+n2);
}

Code mẫu của ll931110

Program NKLETTER;
        Const
                input  = '';
                output = '';
        Var
                s1,s2: string;

Procedure init;
          Var
                f: text;
          Begin
                Assign(f, input);
                        Reset(f);

                Readln(f, s1);
                Readln(f, s2);

                Close(f);
          End;

Procedure solve;
          Var
                f: text;
             max,i,x,y,num: integer;
          Begin
                Assign(f, output);
                        Rewrite(f);

                max:= 0;
                For i:= 1 to length(s1) do if s1[i] = s2[1] then
                        Begin
                                x:= i;
                                y:= 1;
                                num:= 1;

                                While (x < length(s1)) and (y < length(s2)) do
                                        Begin
                                                inc(x);
                                                inc(y);

                                                If s1[x] = s2[y] then inc(num) else break;
                                        End;

                                If x = length(s1) then if max < num then max:= num;
                        End;

                Writeln(f, length(s1) + length(s2) - max);
                Close(f);
          End;

Begin
        init;
        solve;
End.

Code mẫu của skyvn97

program NKLETTER;
var
        pre,suf:ansistring;
        max,lp,ls,len:integer;
begin
        readln(pre);
        readln(suf);
        lp:=length(pre);
        ls:=length(suf);
        max:=0;
        len:=1;
        while (len<=lp) and (len<=ls) do
                begin
                        if copy(pre,lp-len+1,len)=copy(suf,1,len) then max:=len;
                        inc(len);
                end;
        write(lp+ls-max);
end.

Comments

Please read the guidelines before commenting.


There are no comments at the moment.