NOIP 2010 提高組 複賽 第一題 機器翻譯 translate AC代碼(隊列)
總目錄詳見:NOIP 提高組 複賽 試題 目錄 信奧 歷年
在線測評地址:https://www.luogu.com.cn/problem/P1540
隊列寫法如下
手算過程如下:
整個查字典過程如下:每行表示一個單詞的翻譯,冒號前爲本次翻譯後的內存狀況:
1:查找單詞 1 並調入內存。
1 2:查找單詞 2 並調入內存。
1 2:在內存中找到單詞 1。
1 2 5:查找單詞 5 並調入內存。
2 5 4:查找單詞 4 並調入內存替代單詞 1。
2 5 4:在內存中找到單詞 4。
5 4 1:查找單詞 1 並調入內存替代單詞 2。
AC代碼如下:
#include <stdio.h>
int m,n;
int q[1010],h,t;
int main(){
int i,x,j,cnt;
scanf("%d%d",&m,&n);
h=t=1,cnt=0;
for(i=1;i<=n;i++){
scanf("%d",&x);
for(j=h;j<t;j++)//隊列中查找
if(q[j]==x)break;
if(j==t){//內存中未找到
cnt++;
if(t-h==m)h++,q[t]=x,t++;//內存已滿
else q[t]=x,t++;//內存未滿
}
}
printf("%d\n",cnt);
return 0;
}