• UCF Local Programming Contest 2012 G. Lifeform Detector


    链接:https://nanti.jisuanke.com/t/43384

    题意:规定一种字符串替换模式,问给定字符串是否符合该模式

    思路:一开始是想着用递归去判断串是否合法,但是不停WA,查了半天不知道哪有问题,猜测是判断T串的问题

     1 #include<bits/stdc++.h>
     2 #define inf 0x3f3f3f3f
     3 using namespace std;
     4 typedef long long ll;
     5 
     6 const int M = int(1e5) + 5;
     7 const int mod = int(1e9) + 7;
     8 
     9 
    10 string str;
    11 bool Smatch(int s,int e);
    12 bool Tmatch(int s,int e);
    13 
    14 bool match(int s,int e){
    15     if(Smatch(s,e) || Tmatch(s,e)) return true;
    16     else return false;
    17 }
    18 
    19 int main(){
    20     int n;
    21     cin >> n;
    22     for(int k=1;k<=n;k++){
    23         getchar();
    24         getline(cin,str);
    25         cout<<"Pattern "<<k<<": ";
    26         if(match(0,str.size()-1)){
    27             cout<<"More aliens!"<<endl;
    28             cout<<endl;
    29         }
    30         else{
    31             cout<<"Still Looking."<<endl;
    32             cout<<endl;
    33         }
    34     }
    35     
    36     return 0;
    37 }
    38 bool Smatch(int s,int e){
    39     if(e-s+1 == 0) return true;
    40     else if(Smatch(s+1,e) && str[s]=='c') return true;
    41     else if(str[s]=='a'){
    42         int ind=-1;
    43         for(int i=e;i>=s;i--){
    44             if(str[i]=='b') {
    45                 ind=i;
    46                 break;
    47             }
    48         }
    49         if(ind==-1) return false;
    50         else return Tmatch(s+1,ind-1) && Smatch(ind+1,e);
    51     }
    52     else return false;
    53 }
    54 bool Tmatch(int s,int e){
    55     if(Smatch(s+1,e) && str[s]=='c') return true;
    56     else if(str[s]=='a'){
    57         int ind=-1;
    58         for(int i=e;i>=s;i--){
    59             if(str[i]=='b') {
    60                 ind=i;
    61                 break;
    62             }
    63         }
    64         if(ind==-1) return false;
    65         return Tmatch(s+1,ind-1) && Smatch(ind+1,e);
    66     }
    67     else return false;
    68 }
    错误代码

    根据官方题解,本题可采用括号匹配的思路,a为左括号,b为右括号,c为任意字符可跳过,唯一不同的是ab不合法,因为T串不可为空

     1 #include<bits/stdc++.h>
     2 #define inf 0x3f3f3f3f
     3 using namespace std;
     4 typedef long long ll;
     5 
     6 const int M = int(1e5) + 5;
     7 const int mod = int(1e9) + 7;
     8 
     9 stack<char> st;
    10 string sol(string s){
    11     if(s.size() == 0) return "More aliens!";
    12     for(int i=0;i<s.size();i++){
    13         if(s[i] == 'a'){
    14             if(s[i+1] == 'b'){
    15                 return "Still Looking.";
    16             }
    17             else{
    18                 st.push(s[i]);
    19             }
    20         }
    21         else if(s[i] == 'c'){
    22             continue;
    23         }
    24         else if(s[i] == 'b'){
    25             if(st.empty()) return "Still Looking.";
    26             else st.pop();
    27         }
    28         else{
    29             return "Still Looking.";
    30         }
    31     }
    32     if(st.empty()) return "More aliens!";
    33     else return "Still Looking.";
    34 }
    35 int main(){
    36     int n;
    37     cin >> n;
    38     for(int k=1;k<=n;k++){
    39         while(!st.empty()) st.pop();
    40         string s;
    41         cin >> s;
    42         cout << "Pattern " << k << ": " << sol(s) << endl << endl;
    43     }
    44     return 0;
    45 }            
  • 相关阅读:
    前端最全资源汇集
    面试题
    前端知识
    我的工作心得
    一个类似于jq的小型库
    js 模拟css3 动画3
    js 模拟css3 动画2
    js 模拟css3 动画1
    js 模拟css3 动画
    学习node.js 第4篇 建立一个最小的web聊天系统
  • 原文地址:https://www.cnblogs.com/harutomimori/p/12482228.html
Copyright © 2020-2023  润新知