二分贪心 B

题目:

You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.

Input

Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.

Output

Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".

Sample Input

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay

Sample Output

cat
eh
loops

Hint

Huge input and output,scanf and printf are recommended.
分析:
将不同的语言存放到map容器中,在对要查找的的语言进行查找,输出其对应的语言即可。
代码:
#include<iostream>
#include<algorithm>
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{
    multimap<string,string>v;
    multimap<string,string>::iterator it;
    string s1,s2;
    char s[100],m[100],n[100];
    while(gets(s))
    {
        if(!strlen(s))break;
        sscanf(s,"%s %s",m,n);
        s1=m;
        s2=n;
        v.insert(make_pair(s2,s1));
    }
    while(cin>>s1)
    {
        it=v.find(s1);
        if(it!=v.end())
            cout<<(*it).second<<endl;
        else
            cout<<"eh"<<endl;
    }
}
<think> 我们首先需要解释用户提到的五个算法:前缀算法(这里可能是指前缀和)、二分查找、递归、递推算法和贪心算法。注意用户提供了几个引用,但引用内容主要涉及二分查找的非递归实现和一些算法分类,因此我们在回答时应合理参考这些内容,但重点还是在解释算法本身。 1. **前缀算法(前缀和)**:用于快速计算数组区间和。 2. **二分查找**:在有序数组中查找元素的算法。 3. **递归**:函数调用自身的方法。 4. **递推算法**:通过已知条件一步步推导出结果(如动态规划中的递推关系)。 5. **贪心算法**:每一步都选择当前最优解,希望导致全局最优解。 下面按照要求,使用LaTeX公式,代码块,并添加引用(如果适用)。由于用户提供的引用[1]和[2]都是关于二分查找的非递归实现,而引用[3]提到了前缀和、递归、贪心等,因此我们可以适当引用。 注意:用户要求生成相关问题,放在最后。 我们将按照以下结构组织回答: - 每个算法单独一小节,包括原理、实现步骤(代码示例,语言可选Java或Python)、特性(时间复杂度和空间复杂度)等。 - 使用表格对比这些算法的特点(如果有可比性)。 - 最后生成相关问题。 由于引用[3]中提到了前缀和与差分,我们重点参考。 </think> ### 前缀和算法(Prefix Sum) **原理**:通过预处理构建**累加数组**,实现区间和的$O(1)$查询。设原数组为$a$,前缀和数组$S$满足: $$S[i] = \sum_{k=0}^{i} a[k]$$ 则区间$[l,r]$的和为$S[r] - S[l-1]$[^3]。 **实现示例**: ```python def prefix_sum(arr): s = [0] * (len(arr)+1) for i in range(1, len(arr)+1): s[i] = s[i-1] + arr[i-1] # 累积计算 return s # 查询arr[2:5]的和 arr = [3, 1, 4, 2, 5] s = prefix_sum(arr) print(s[5] - s[2]) # 输出 4+2+5=11 ``` **特性**: - 时间复杂度:预处理$O(n)$,查询$O(1)$ - 空间复杂度:$O(n)$ - 应用场景:频繁区间求和(如股票区间收益统计) --- ### 二分查找(Binary Search) **原理**:在**有序数组**中通过**折半比较**快速定位元素。每次排除一半数据,时间复杂度$O(\log n)$[^1][^2]。 **非递归实现**(引用自[^2]): ```java public static int binarySearch(int[] arr, int target) { int left = 0, right = arr.length - 1; while (left <= right) { int mid = left + (right - left) / 2; // 防溢出 if (arr[mid] == target) return mid; else if (arr[mid] > target) right = mid - 1; // 左半区 else left = mid + 1; // 右半区 } return -1; // 未找到 } ``` **特性**: - 时间复杂度:$O(\log n)$ - 前提条件:数据有序 - 变体应用:寻找边界值(如第一个大于x的元素) --- ### 递归算法(Recursion) **原理**:通过**函数自调用**将问题分解为同构子问题,需定义: 1. 递归基(终止条件) 2. 递归关系(问题分解式) **斐波那契数列示例**: ```python def fib(n): if n <= 1: return n # 递归基 return fib(n-1) + fib(n-2) # 递归关系 ``` **核心要点**: - 时间复杂度:与递归深度相关(斐波那契为$O(2^n)$) - 空间复杂度:$O(n)$(调用栈深度) - 适用场景:树遍历、分治算法等[^3] --- ### 递推算法(Iterative Recurrence) **原理**:通过**迭代计算**实现递归关系,避免栈溢出。将自顶向下递归改为自底向上递推。 **斐波那契优化**: ```python def fib_iter(n): a, b = 0, 1 for _ in range(n): a, b = b, a+b # 状态迭代 return a ``` **对比递归优势**: - 时间复杂度:$O(n)$(优于递归的$O(2^n)$) - 空间复杂度:$O(1)$(无需调用栈) - 应用场景:动态规划状态转移[^3] --- ### 贪心算法(Greedy Algorithm) **原理**:每步选择**局部最优解**,期望得到全局最优解,需满足贪心选择性质[^3]。 **找零钱示例**(硬币面值[1,5,10]): ```python def coin_change(amount): coins = [10, 5, 1] count = 0 for c in coins: while amount >= c: # 优先选最大面值 amount -= c count += 1 return count ``` **特性与局限**: - 时间复杂度:通常$O(n\log n)$(需排序) - 不保证全局最优:若硬币面值为[1,3,4],贪心法解amount=6可能得4+1+1(非最优解3+3) - 适用场景:霍夫曼编码、最小生成树(Prim算法) --- ### 算法对比总结 | 特性 | 前缀和 | 二分查找 | 递归 | 递推 | 贪心算法 | |--------------|-------------|-------------|-------------|-------------|--------------| | **核心思想** | 空间换时间 | 分治 | 自顶向下分解 | 自底向上计算 | 局部最优 | | **时间复杂度**| $O(n)$预处理 | $O(\log n)$ | 依赖问题结构 | 通常$O(n)$ | 通常$O(n\log n)$ | | **空间复杂度**| $O(n)$ | $O(1)$ | $O(n)$ | $O(1)$ | $O(1)$ | | **关键条件** | 无 | 数据有序 | 递归基/关系 | 状态转移方程 | 贪心选择性质 | > **注**:前缀和与差分互为逆运算(引用[3]中关联),递归与递推可相互转化;贪心算法需严格证明最优性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值