URAL 1089|Verification with a Vocabulary|暴力

30 篇文章 0 订阅

http://acm.timus.ru/problem.aspx?space=1&num=1089

题目

你的英语老师跟你说她最近梦想有一个自动化的系统去批改小学生的作业,并统计有多少个单词拼错了。3月8号就要到了,你想写一个程序实现老师的想法,当做礼物送给老师,这样老师在考试打分的时候就可以照顾一下你。程序需要实现:替换错误的单词(正确的单词拼写列表已给出,单词拼写错误不超过一个字母),统计拼错的单词数。你的英语老师忘了小学生还可能漏字母或多一个字母,但是你的程序不用考虑这些,因为老师忘记提这个要求了。

输入

输入文件由2部分组成,第一部分每行一个单词表示单词表内的正确单词(每个单词不超过8个字母),第二部分许多行表示一个小学生写的几个句子(整个文本不超过1000个单词),所有单词都只包含小写字母。不会出现在单词表内找不到对应单词、或者存在2种或以上正确单词对应的情况。

输出

输出修正的正确文本,并输出一行一个整数表示更正单词的个数。

样例输入

country
occupies
surface
covers
russia
largest
europe
part
about
world
#
the rushia is the larjest cauntry in the vorld.
it ockupies abaut one-seventh of the earth's surfase.
it kovers the eastern park of yurope and the northern park of asia.

样例输出

the russia is the largest country in the world.
it occupies about one-seventh of the earth's surface.
it covers the eastern part of europe and the northern part of asia.
11

题解

暴力。。

#include <iostream>
#include <vector>
#include <string>
#define rep(i,j,k) for(i=j;i<k;++i)
using namespace std;

inline void nextAlphabet(const string &str, int &i) {
    while (i < str.length() && (str[i] < 'a' || str[i] > 'z'))
        ++i;
}

// returns the end index of current word, excluded.
inline int skimWord(const string &str, int from) {
    while (from < str.length() && str[from] >= 'a' && str[from] <= 'z')
        ++from;
    return from;
}

int main() {
    vector<string> vocabulary;

    // read vocabulary.
    string line;
    while (true) {
        getline(cin, line);
        if (line == "#") break;
        else vocabulary.push_back(line);
    }

    // read text, since lines can be merged.
    string text = "";
    int ans = 0, i = 0, j, k;
    while (getline(cin, line)) text += line + '\n';

    // for each words in the text, check the minimum distance to a word in vocabulary and correct them.
    while (true) {
        nextAlphabet(text, i);
        // no more words
        if (i >= text.length()) break;

        int to = skimWord(text, i);
        string word = text.substr(i, to - i);

        // search the vocabulary
        rep(j,0,vocabulary.size()) {
            string correct = vocabulary[j];
            if (correct.length() == word.length()) {
                int d = -1;
                rep(k,0,word.length())
                    if (vocabulary[j][k] != word[k]) {
                        if (d == -1)
                            d = k;
                        else {
                            d = -1;
                            break;
                        }
                    }
                if (d != -1) {
                    ++ans;
                    text[i + d] = correct[d];
                    break;
                }
            }
        }
        i = to;
    }
    cout << text << ans << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值