返回列表 發帖

c462: apcs 交錯字串 解答

本帖最後由 ray 於 2020-7-11 11:16 編輯
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;

  4. int main()
  5. {
  6.     int k,max,l,len,type;
  7.     string str;
  8.     cin>>k>>str;
  9.     max = l = len = 0;
  10.     for(int i=0;i<str.length();i++)
  11.     {
  12.         int t = isupper(str[i]);
  13.         if(i == 0)
  14.             type = t;
  15.         
  16.         if(t == type)
  17.         {
  18.             l++;
  19.             if(l > k)
  20.                 len = k;
  21.             //cout<<str[i]<<"==/"<<endl;
  22.         }
  23.         else
  24.         {
  25.             if(l < k)
  26.                 len=0;
  27.             l = 1;
  28.             //cout<<str[i]<<"<>/"<<endl;
  29.         }
  30.         if(l == k)
  31.             len += k;
  32.         if(len > max)
  33.             max = len;
  34.         //cout<<"len="<<len<<"/l="<<l<<endl;
  35.         type = t;
  36.     }
  37.     cout<<max<<endl;
  38.     //system("pause");
  39.     return 0;
  40. }
複製代碼

返回列表