Editorial for Word Counting
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.
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 c,lc:char; t,i,cur,pre,j:byte; let:set of char; max,dem:integer; begin let:=[]; for c:='a' to 'z' do let:=let+[c]; readln(t); for i:=1 to t do begin cur:=0; max:=1; dem:=0; repeat read(c); while (ord(c)=32) and (ord(lc)=32) do read(c); if c in let then inc(cur) else begin if cur=pre then inc(dem) else begin if dem>max then max:=dem; dem:=1; end; pre:=cur; cur:=0; end; lc:=c; until eoln; if cur=pre then inc(dem); if dem>max then max:=dem; readln; writeln(max); end; end.
Code mẫu của happyboy99x
#include <iostream> #include <sstream> #include <string> using namespace std; int main() { string line, s; int t, best, len, prlen, wrdcnt; ios::sync_with_stdio(false); cin >> t; getline(cin, s); while(t--) { getline(cin, line); best = prlen = wrdcnt = 0; stringstream ss(line); while(ss >> s) { len = s.size(); if(len==prlen) wrdcnt++; else { if(wrdcnt > best) best = wrdcnt; wrdcnt = 1; prlen = len; } } if(wrdcnt > best) best = wrdcnt; cout << best << endl; } return 0; }
Code mẫu của ladpro98
program wordcnt; uses math; const fi=''; var t,d,temp,res,i,j,k:longint; s:ansistring; inp:text; function getlength:longint; var kq:longint; begin kq:=0; while s[j]<>' ' do begin inc(kq); inc(j); end; exit(kq); end; begin assign(inp,fi); reset(inp); readln(inp,t); for i:=1 to t do begin res:=0; readln(inp,s); s:=s+' '; temp:=1; j:=1; t:=getLength; d:=t; while j<length(s) do begin while (j<length(s)) and (s[j]=' ') do inc(j); t:=getLength; if t=d then inc(temp) else begin temp:=1; d:=t; end; res:=max(res,temp); end; res:=max(res,temp); writeln(res); end; end.
Code mẫu của RR
uses math; var test:longint; s:ansistring; f,a:array[0..1011] of longint; n,i,res:longint; begin readln(test); for test:=1 to test do begin readln(s); while s[1]=' ' do delete(s,1,1); while pos(' ',s)>0 do delete(s,pos(' ',s),1); while s[length(s)]=' ' do delete(s,length(s),1); s:=s+' '; n:=0; while pos(' ',s)>0 do begin inc(n); a[n]:=pos(' ',s)-1; delete(s,1,pos(' ',s)); end; res:=0; for i:=1 to n do begin if a[i]=a[i-1] then f[i]:=f[i-1]+1 else f[i]:=1; res:=max(res,f[i]); end; writeln(res); end; end.
Code mẫu của hieult
import java.io.*; //import java.util.*; public class Main { public static void main(String args[]) throws IOException { InputStreamReader readin = new InputStreamReader(System.in); BufferedReader read = new BufferedReader(readin); String s; String [] temp; int n; s = read.readLine(); n = Integer.parseInt(s); for(int ii = 1;ii<=n;ii++) { s = read.readLine(); temp = s.split(" "); int t = 1, max = 1 , a=0; a = temp[0].trim().length(); for(int i = 1;i<temp.length;i++) { if(temp[i].trim().length()==0) continue; else if(a==0) { t = 1; a =temp[i].trim().length(); } else if(temp[i].trim().length()==a) t++; else { t = 1; a =temp[i].trim().length(); } if(t>max) max = t; } System.out.println(max); } } }
Code mẫu của ll931110
Program WORDCNT; Const input = ''; output = ''; Var s: string; i,t,n: integer; fi,fo: text; Procedure init; Begin Readln(fi, s); n:= length(s); End; Procedure solve; Var max,i,clen,cword,len: integer; Begin max:= 0; clen:= -1; cword:= 1; len:= 0; For i:= 1 to n do If s[i] <> ' ' then inc(len) else Begin If len = clen then inc(cword) else Begin If max < cword then max:= cword; If len <> 0 then Begin clen:= len; cword:= 1; End; End; len:= 0; End; If len = clen then inc(cword); If (max < cword) and (clen <> 0) then max:= cword; Writeln(fo, max); End; Begin Assign(fi, input); Reset(fi); Assign(fo, output); Rewrite(fo); Readln(fi, t); For i:= 1 to t do Begin init; solve; End; Close(fo); Close(fi); End.
Comments