VMSUBSTR - Vườn cây của ba

Tác giả: ladpro98

Ngôn ngữ: Pascal

program VMSUBSTR;
uses    math;
const   fi='';
var     n,qq,t,i,res:longint;
        a,s:ansistring;
        p,q:char;
        chk:array['A'..'z'] of boolean;
        c:array['A'..'z','A'..'z'] of longint;
        inp:text;

begin
        assign(inp,fi);reset(inp);
        readln(inp,n);readln(inp,a);
        for i:=2 to n do
        begin
                inc(c[a[i],a[i-1]]);
                inc(c[a[i-1],a[i]]);
        end;
        readln(inp,qq);
        for t:=1 to qq do
        begin
                readln(inp,s);
                res:=0;
                for i:=1 to length(s) do chk[s[i]]:=true;
                if chk[a[1]] then inc(res);
                if chk[a[length(a)]] then inc(res);
                for p:='A' to 'z' do
                for q:=chr(ord(p)+1) to 'z' do
                if chk[p]<>chk[q] then inc(res,c[p,q]);
                writeln(res shr 1);
                for i:=1 to length(s) do chk[s[i]]:=false;
        end;
end.

Download