比赛 顾研NOIP2011模拟赛 评测结果 AAAAAAAAAT
题目名称 项链 最终得分 90
用户昵称 4154 运行时间 2.001 s
代码语言 Pascal 内存使用 0.17 MiB
提交时间 2012-10-17 19:10:17
显示代码纯文本
program necklaced;

const statenotchoose=1;
      statechoose=-1;
      statenotsure=0;

var n,sum,ans:integer;
    a:array[1..26]of string;
    f:array[1..26,'A'..'Z']of boolean;
    final,ns,s:array['A'..'Z']of integer;

    fla:boolean;


procedure sort(var a:string);
var i,j,k:integer;
    chr2:char;
begin
  for i:=1 to length(a) do
  begin
    k:=i;
    for j:=i+1 to length(a) do
      if a[j]<a[k] then k:=j;
    chr2:=a[i];
    a[i]:=a[k];
    a[k]:=chr2;
  end;
end;


procedure init;
var i,j:integer;
    chr1:char;
begin
  assign(input,'necklaced.in');
  reset(input);
  readln(n);
  for i:=1 to n do
    readln(a[i]);
  close(input);

  for i:=1 to n do
    for j:=1 to length(a[i]) do
    begin
      inc(s[a[i][j]]);
      f[i,a[i][j]]:=true;
    end;

  for i:=1 to n do
    sort(a[i]);

  for chr1:='A' to 'Z' do
  begin
    final[chr1]:=0;
    for i:=n downto 1 do
      if f[i,chr1] then begin
        final[chr1]:=i;
        break;
      end;
    if final[chr1]=0 then final[chr1]:=n+1;
  end;
end;





procedure dfs(k:integer);
var state,i,j:integer;
    chr1,chr2:char;

begin
  if k=n+1 then
  begin
    if ans>=sum then exit;
    fla:=true;
    for chr1:='A' to 'Z' do
      if odd(ns[chr1])
       then begin
         fla:=false;
         break;
       end;
    if fla then ans:=sum;

    exit;
  end;


  for chr1:='A' to 'Z' do
    if final[chr1]=k then
    begin
      if odd(ns[chr1]) then state:=statechoose
      else state:=statenotchoose;
    end else state:=statenotsure;
  if (state=statenotchoose)or(state=statenotsure) then
  begin
    dfs(k+1);
  end;
  if (state=statechoose)or(state=statenotsure) then
  begin
    inc(sum);
    for i:=1 to length(a[k]) do
      inc(ns[a[k][i]]);

    dfs(k+1);

    dec(sum);
    for i:=1 to length(a[k]) do
      dec(ns[a[k][i]]);
  end;
end;


procedure outans;
begin
  assign(output,'necklaced.out');
  rewrite(output);
  writeln(ans);
  close(output);
end;


begin
  init;
  dfs(1);
  outans;


end.