# Stack&2分&并查集&Dp&Greddy_081007day2

153人阅读 评论(0)

今天做了关于用栈处理字符串、并查集、二分查找、Dp、Tire的题目，难度并不大，关键是细节。总结如下：

1、调试的时候，Fpc Doc窗口下一行只能粘贴256个字符。Debug时没有注意，导致输入不完整，输出错误答案，弄得我还以为是程序错了==最后才发现是输入不完整造成的，太坑爹了==|||

2、在cpp字符串处理中，string有时候比char[]更好用，值得好好研究研究。

rank.pas_code:

type Stu=record
s:string;
f:longint;
end;
var
n,m,i,j,k,ans,t:longint;
a:array[1..10000] of Stu;
st:string;
ch:char;
procedure qsort(l,r:longint);
var
i,j:longint;
x,y:Stu;
begin
i:=l;
j:=r;
x:=a[random(r-l+1)+l];
repeat
while a[i].s<x.s do inc(i);
while x.s<a[j].s do dec(j);
if not(i>j) then begin
y:=a[i]; a[i]:=a[j]; a[j]:=y;
inc(i);
dec(j);
end;
until i>j;
if i<r then qsort(i,r);
if l<j then qsort(l,j);
end;
function search(st:string):longint;
var
l,r,mid:longint;
begin
l:=1;
r:=n;
while (r>l) do begin
mid:=(l+r)shr 1;
if a[mid].s=st then exit(mid);
if st<a[mid].s then r:=mid-1
else l:=mid+1;
end;
exit(l);
end;

Begin
assign(input,'rank.in');        reset(input);
assign(output,'rank.out');      rewrite(output);
randomize;
readln(n);
for i:=1 to n do readln(a[i].s);
qsort(1,n);
k:=search('LazyChild');
readln(m);
for i:=1 to m do begin
for j:=1 to n do begin
read(t);
read(ch);
readln(st);
inc(a[search(st)].f,t);
end;
ans:=0;
for j:=1 to n do
if a[j].f>a[k].f then inc(ans);
writeln(ans+1);
end;
close(input);   close(output);
end.

language.pas_code:

const
cset=['0'..'9','+','-','*','/','=','(',')',' ',#10,#13];
var
a,s:array[1..10000] of char;
i,l:longint;
t:longint;
procedure Error;
var j:longint;
begin
writeln('NO');
close(input);   close(output);
halt;
end;
Begin
assign(input,'language.in');    reset(input);
assign(output,'language.out');  rewrite(output);
l:=0;
while (not EOF) do begin
inc(l);
read(a[l]);
end;
t:=0;
i:=1;
while i<=l do begin
case a[i] of
'(':begin
inc(t);
s[t]:=a[i];
end;
')':begin
if t=0 then Error;
dec(t);
end;
'*':if (i>1) and (a[i-1]='(') then
begin
dec(t);
i:=i+2;
while (i<=l) and ( (a[i-1]<>'*')or(a[i]<>')') )  do inc(i);
if i>l then Error;
end;
else if (t>0) and (not(a[i] in cset)) then Error;
end;
inc(i);
end;
if t>0 then Error;
writeln('YES');
close(input);   close(output);
end.


network.pas_code:

var
n,d,i,x,y:longint;
f:array[1..1001] of longint;
fix:array[1..1001] of boolean;
a:array[1..1001,0..1] of longint;
ch:char;
function find(k:longint):longint;
begin
if f[k]=k then exit(k);
f[k]:=find(f[k]);
exit(f[k]);
end;
procedure union(x,y:longint);
var e1,e2:longint;
begin
e1:=find(x);
e2:=find(y);
if x<>y then
f[e1]:=e2;
end;
Begin
assign(input,'network.in');     reset(input);
assign(output,'network.out');   rewrite(output);
readln(n,d);
for i:=1 to n do begin readln(a[i,0],a[i,1]); f[i]:=i; end;

while not EOF do begin
read(ch);
if ch='O' then begin
readln(x);
fix[x]:=true;
for i:=1 to n do
if fix[i] and (sqr(a[i,0]-a[x,0])+sqr(a[i,1]-a[x,1])<=d*d)
then union(i,x);
end
else begin
readln(x,y);
if find(x)=find(y) then writeln('SUCCESS')
else writeln('FAIL');
end;
end;
close(input);   close(output);
end.



word.cpp_code:

/*
Stack算法，O(nlgn+Len)
*/
#include<iostream>
using namespace std;
const int MXN=10100;
string s[MXN];
char t[MXN*31];
int p[MXN],top=1;
int main()
{
freopen("word.in","r",stdin);
freopen("word.out","w",stdout);
scanf("%s",t);
int n=0,i,l=strlen(t),ans=1;
for(i=0;i<l;i++) if (t[i]==','||t[i]=='.') n++;
else s[n]+=t[i];
sort(s,s+n);
for(i=1;i<n;i++) if (s[i]!=s[i-1]) {
while (top>0&&s[i].find(s[p[top-1]])!=0) top--;
p[top++]=i;
if (top>ans) ans=top;
}
cout<<ans<<endl;
}


word.pas_code:

(*
Tire,O(len+n)
*)
const
nn = 200001;

type
trieNode = record
son: array['a'..'z']of longint;
flag: boolean;
end;

var
n, i, best, tot, num: longint;
trie: array[0 .. nn]of trieNode;
s : array[0 .. 10001] of string;
ff : boolean;
ch : char;

procedure init;
var
ch: char;
begin
tot := 0;
for ch:='a' to 'z' do trie[0].son[ch] := 0;
trie[0].flag := false;
end;

procedure add(s: string);
var
p, i: longint;
begin
p := 0;
for i:=1 to length(s) do begin
if trie[p].son[s[i]] = 0 then begin
inc(tot);
trie[p].son[s[i]] := tot;
end;
p := trie[p].son[s[i]];
end;
trie[p].flag := true;
end;

procedure dfs(x: longint);
var
ch: char;
f: boolean;
begin
if trie[x].flag then inc(num);
f := true;
for ch:='a' to 'z' do
if trie[x].son[ch] <> 0 then begin
f := false;
dfs(trie[x].son[ch]);
end;
if f then
if num > best then
best := num;
if trie[x].flag then dec(num);
end;

begin
assign(input,'word.in'); reset(input);
assign(output,'word.out'); rewrite(output);
n := 0;
repeat
inc(n);
ff := false;
s[n] := '';
repeat
read(ch);
if ch = '.' then begin
ff := true;
break;
end;
if ch = ',' then
break;
s[n] := s[n] + ch;
until false;
if ff then
break;
until false;
init;
for i:=1 to n do
add(s[i]);
best := 0;
num := 0;
dfs(0);
writeln(best);
close(input); close(output);
end.


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：2743次
• 积分：74
• 等级：
• 排名：千里之外
• 原创：6篇
• 转载：1篇
• 译文：0篇
• 评论：0条