zoukankan      html  css  js  c++  java
  • [POJ1496]Word Index

    Word Index
    Time Limit: 1000MS
    Memory Limit: 10000K
    Total Submissions: 3200
    Accepted: 1831

    Description

    Encoding schemes are often used in situations requiring encryption or information storage/transmission economy. Here, we develop a simple encoding scheme that encodes particular types of words with five or fewer (lower case) letters as integers.

    Consider the English alphabet {a,b,c,...,z}. Using this alphabet, a set of valid words are to be formed that are in a strict lexicographic order. In this set of valid words, the successive letters of a word are in a strictly ascending order; that is, later letters in a valid word are always after previous letters with respect to their positions in the alphabet list {a,b,c,...,z}. For example,

    abc aep gwz

    are all valid three-letter words, whereas

    aab are cat

    are not.

    For each valid word associate an integer which gives the position of the word in the alphabetized list of words. That is:

    a -> 1

    b -> 2

    .

    .

    z -> 26

    ab -> 27

    ac -> 28

    .

    .

    az -> 51

    bc -> 52

    .

    .

    vwxyz -> 83681

    Your program is to read a series of input lines. Each input line will have a single word on it, that will be from one to five letters long. For each word read, if the word is invalid give the number 0. If the word read is valid, give the word's position index in the above alphabetical list.

    Input

    The input consists of a series of single words, one per line. The words are at least one letter long and no more that five letters. Only the lower case alphabetic {a,b,...,z} characters will be used as input. The first letter of a word will appear as the first character on an input line.

    The input will be terminated by end-of-file.

    Output

    The output is a single integer, greater than or equal to zero (0) and less than or equal 83681. The first digit of an output value should be the first character on a line. There is one line of output for each input line.

    Sample Input

    z
    a
    cat
    vwxyz

    Sample Output

    26
    1
    0
    83681

    Source

    我的解法:
    先生成所有代码的字典,然后读入,再二分查找
    const max=83681;
    var s,s0:string;
    s1:
    array[1..5] of char;
    len,i,j:integer;
    index,k:longint;
    a:
    array[1..max] of string[5];

    function valid(s:string):boolean;
    var i:integer;
    begin
    for i:=2 to len do
    if s[i]<=s[i-1] then exit(false);
    exit(true);
    end;
    //生成字典
    procedure find(t,l:integer);
    var i:char;
    begin
    if t>l then
    begin
    inc(index);
    s0:
    ='';
    for j:=1 to t-1 do
    s0:
    =s0+s1[j];
    a[index]:
    =s0;
    exit;
    end;
    for i:='a' to 'z' do
    if (t=1) or (i>s1[t-1]) then
    begin
    s1[t]:
    =i;
    find(t
    +1,l);
    end;
    end;
    //判定字符串s1不小于s2
    function notsmall(s1,s2:string):boolean;
    var i:integer;
    begin
    if (length(s1)>length(s2)) or ((length(s1)=length(s2)) and (s1>=s2)) then exit(true)
    else exit(false);
    end;

    //二分法获取字符串index
    function getIndex(s:string;l,r:longint):longint;
    var i,k:longint;
    begin
    if l=r then exit(l)
    else
    begin
    k:
    =(l+r) div 2;
    if notsmall(a[k],s) then getIndex:=getIndex(s,l,k)
    else getIndex:=getIndex(s,k+1,r);
    end;
    end;

    begin
    for i:=1 to 5 do
    find(
    1,i);
    while not EOF do
    begin
    readln(s);
    len:
    =length(s);
    if valid(s) then writeln(getIndex(s,1,max))
    else writeln(0);
    end;
    end.
  • 相关阅读:
    CSS3 -- 透明色(rgba)
    CSS3 -- 多背景(backgrounds)
    CSS3 -- 背景原点(background-origin)
    CSS3 -- 背景裁剪(background-clip)
    CSS3 -- 背景尺寸(background-size)
    CSS3 -- 图片边框(border-image)
    CSS3 -- 边框颜色(border-color)
    CSS3 -- 盒模型(box-sizing)
    CSS -- 字体单位(px、em、rem)
    课程总结
  • 原文地址:https://www.cnblogs.com/nbalive2001/p/2015595.html
Copyright © 2011-2022 走看看