10 条题解

  • 16
    @ 2023-6-9 18:39:14
    思路 开了一个队列 q 模拟内存情况,数组 inq 来判断每个单词是否在内存中,ans记录答案,处理好每次的进队和出队时q和inq数组的变化。当每次需要去外存查找就ans++。具体细节见注释。
    代码
    
    #include<iostream>
    #include<queue>
    using namespace std;
    queue<int> q;//队列模拟内存情况
    int m,n,ans;
    bool inq[1010];//判断单词是否在内存中
    int main()
    {
    	cin>>m>>n;
    	for(int i=1;i<=n;i++)
    	{
    		int x;
    		cin>>x;
    		if(inq[x])continue;//能够在内存中查找就跳过
    		else
    		{
            	//如果内存满了,最早进入内存的那个单词出列
    			if(q.size()>=m)
    			{
    				inq[q.front()]=false;
    				q.pop();
    			}
                //把外存的结果加入内存以便下次查找
    			q.push(x);
    			inq[x]=true;
    			ans++;
    		}
    	}
    	cout<<ans;
    	return 0;
    }
    
    
    • 6
      @ 2024-1-31 19:44:03
      和12章节的一样,复制过来就行了
      AC代码:
      #include <bits/stdc++.h>
      using namespace std;
      int m, n, q[1005], h, t, ans, x;
      bool ing[1005];
      int main()
      {
      	cin >> m >> n;
      	t = 1, h = 1;
      	while (n--)
      	{
      		cin >> x;
      		if (!ing[x])
      		{
      			q[t] = x;
      			t++;
      			ing[x] = true;
      			if (t - h > m)
      			{
      				ing[q[h]] = false;
      				h++;
      			}
      			ans++;
      		}
      	}
      	cout << ans;
      	return 0;
      }
      
      • 6
        @ 2023-6-10 0:19:47

        题解三四条,细节第一条 若是没有赞,作者两行泪!!!

        #include <iostream>
        #include <stdio.h>
        #include <algorithm>
        using namespace std;
        int n,m,x,ans,l,r,a[1005],b[1005];
        int main()
        {
            cin>>m>>n;
            l=0;r=0;
            for (int i=1;i<=n;i++)
             {
                 while(1){cout<<"你居然抄题解!!!!“<<endl;
                 scanf("%d",&x);
                 if (a[x]==0) 
                 {
                     ans++;
                    r++;b[r]=x;a[x]=1;
                    if (r>m) {l++;a[b[l]]=0;}
                 }
             }
            cout<<ans;
            return 0;
        }
        
      • 3
        @ 2023-11-4 17:22:28

        直接用12课的代码及可。

        • 3
          @ 2023-10-27 19:52:08

          这是一道雷同题(跟第12章一样的)

          #include<bits/stdc++.h>
          using namespace std;
          int m,n,a[1005],sum;
          int que[100005],head=1,tail;
          bool book(int x){
              for (int j=head;j<=tail;j++)if (x==que[j])return 0;
              return 1;
          }
          int main(){
              cin>>m>>n;
              for (int i=1;i<=n;i++)cin>>a[i];
              for (int i=1;i<=n;i++)if (book(a[i])){
                  if (tail-head+1==m)head++;
                  que[++tail]=a[i];
                  sum++;
              }
              cout<<sum;
              return 0;
          }
          
          • 0
            @ 2023-10-28 14:57:52

            这是一个典型的模拟题,可以通过模拟翻译的过程来计算查字典的次数。

            #include <iostream>
            #include <vector>
            #include <unordered_map>
            
            using namespace std;
            
            int main() {
                int M, N;
                cin >> M >> N;
                vector<int> words(N);
                for (int i = 0; i < N; ++i) {
                    cin >> words[i];
                }
                unordered_map<int, int> dict;
                int cnt = 0;
                for (int i = 0; i < N; ++i) {
                    if (dict.find(words[i]) != dict.end()) {
                        continue;
                    }
                    if (i + 1 < N && words[i] == words[i + 1]) {
                        ++i;
                        continue;
                    }
                    if (dict.size() < M) {
                        dict[words[i]] = i;
                    } else {
                        int first = *dict.begin();
                        dict.erase(first);
                        dict[words[i]] = i;
                        cnt += M - dict[first];
                    }
                }
                cout << cnt << endl;
                return 0;
            }
            

            这段代码首先读取内存容量和文章长度,然后读取文章中的单词并建立一个哈希表来存储已经翻译过的单词。在翻译的过程中,如果哈希表中已经存在该单词,则直接跳过;否则,将该单词及其在文章中的位置存入哈希表。如果哈希表已满,则删除最早存入的单词,然后将新单词存入哈希表。最后输出查字典的次数。

            • -1
              @ 2024-2-20 22:24:00
              #include <bits/stdc++.h>
              using namespace std;
              queue<int> q;
              bool a[1005];
              int main()
              {
                  int n,m;
                  int ans = 0;
                  cin >> m >> n;
                  int x;
                  for (int i = 1;i <= n;i++)
                  {
                      cin >> x;
                      if (int(q.size())< m)
                      {
                          if (a[x] == 1)
                          {
                              continue;
                          }
                          else
                          {
                              ans++;
                              q.push(x);
                              a[x] = 1;
                          }
                      }
                      else
                      {
                          if (a[x] == 1)
                          {
                              continue;
                          }
                          else
                          {
                              ans++;
                              a[q.front()] = 0;
                              q.pop();
                              q.push(x);
                              a[x] = 1;
                          }
                      }
                  }
                  cout << ans;
                  return 0;
              }
              
              • -1
                @ 2024-1-15 18:56:22

                简单一个题解

                
                

                #include<bits/stdc++.h> using namespace std; int m,n,a[1005],sum; int que[100005],h=1,t; #define w while(1) #define pt printf(h) #define int short int bool book(int x){

                for (int j=h;j<=t;j++)if (x==que[j])return 0;
                return 1;
                

                } int main(){ cin>>m>>n; for (int i=1;i<=n;i++)cin>>a[i];

                for (int i=1;i<=n;i++)if (book(a[i])){
                    if (t-h+1==m)h++;
                    que[++t]=a[i];
                    sum++;
                }
                cout<<sum;
                
                return 0;
                

                }

                
                
                • -5
                  @ 2023-6-17 22:04:04

                  #include<bits/stdc++.h> using namespace std; queue<int> q; int m,n,ans; bool inq[1010]; int main(){ cin>>m>>n; for(int i=1;i<=n;i++){ int x; cin>>x; if(inq[x])continue; else{ if(q.size()>=m){ inq[q.front()]=false; q.pop(); } q.push(x); inq[x]=true; ans++; } } cout<<ans; return 0; }

                  • 1

                  信息

                  ID
                  144
                  时间
                  1000ms
                  内存
                  256MiB
                  难度
                  3
                  标签
                  (无)
                  递交数
                  1114
                  已通过
                  630
                  上传者