• Hidden String(深搜)


    Hidden String

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1679    Accepted Submission(s): 591

    Problem Description
    Today is the 1st anniversary of BestCoder. Soda, the contest manager, gets a string s of length n. He wants to find three nonoverlapping substrings s[l1..r1], s[l2..r2], s[l3..r3] that:
    1. 1l1r1<l2r2<l3r3n
    2. The concatenation of s[l1..r1], s[l2..r2], s[l3..r3] is "anniversary".
     
    Input
    There are multiple test cases. The first line of input contains an integer T (1T100), indicating the number of test cases. For each test case:
    There's a line containing a string s (1|s|100) consisting of lowercase English letters.
     
    Output
    For each test case, output "YES" (without the quotes) if Soda can find such thress substrings, otherwise output "NO" (without the quotes).
     
    Sample Input
    2 annivddfdersewwefary nniversarya
     
    Sample Output
    YES NO
     

    题意:给你一个串,要匹配anniversary,字段数不得大于3;

    题解:吐槽一下,为毛是大于等于11就ac,大于等于12就wa,错了N次。。。。。明明长度是11但是就应该到12

    的啊。。。

    思路:从当前开始向后匹配;匹配完成就往下深搜,当匹配段数大于3

    的时候就结束当前深搜。。。

    代码:

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #include<vector>
    #include<map>
    #include<algorithm>
    using namespace std;
    #define mem(x,y) memset(x,y,sizeof(x))
    #define SI(x) scanf("%d",&x)
    #define SL(x) scanf("%lld",&x)
    #define PI(x) printf("%d",x)
    #define PL(x) printf("%lld",x)
    #define P_ printf(" ")
    #define T_T while(T--)
    typedef long long LL;
    const int INF=0x3f3f3f3f;
    char s[110];
    char a[20]="anniversary";
    int ans,len;
    void dfs(int p1,int p2,int num){
    	if(num>3)return;
    	if(p2>=11){
    	//	printf("%d
    ",num);
    		ans=1;return;
    	}
    //	printf("%d
    ",len);
    //	if(p1>len)return;
    	int x,y;
    	for(int i=p1;i<len;i++){
    	x=i;y=p2;
    		while(s[x]==a[y])x++,y++;
    	//	printf("%d",y);
    		 if(x!=i)dfs(x,y,num+1);
    		 else dfs(x+1,y,num+1);
    	}
    }
    int main(){
    	int T;
    	SI(T);
    	T_T{
    		ans=0;
    		scanf("%s",s);
    		len=strlen(s);
    		dfs(0,0,0);
    		if(ans)puts("YES");
    		else puts("NO");
    	}
    	return 0;
    }
    

    java:

    package com.lanqiao.week1;
    
    import java.util.Scanner;
    
    public class hdu5311 {
        private static Scanner cin = null;
        static{
            cin = new Scanner(System.in);
        }
        static char[] mstr = "anniversary".toCharArray();
        static boolean ans;
        private static void dfs(int m, int s, int cnt, char[] str){
            
            //System.out.println(m + "-->" + s + "-->" + cnt);
            if(cnt > 3)return;
            if(m >= mstr.length){
                ans = true;
                return;
            }
            for(int i = s; i < str.length; i++){
                int si = i, mi = m;
                while(mi < mstr.length && si < str.length && mstr[mi] == str[si]){
                    mi++;
                    si++;
                }
                if(si != i){
                    dfs(mi, si, cnt + 1, str);
                }
                
            }
        }
        
        public static void main(String[] args) {
            int T;
            T = cin.nextInt();
            while(T-- > 0){
                String str = cin.next();
                ans = false;
                dfs(0, 0, 0, str.toCharArray());
                
                if(ans){
                    System.out.println("YES");
                }else{
                    System.out.println("NO");
                }
            }
        }
    }
  • 相关阅读:
    文件上传之前先进行预览
    代码规范
    ESLint 配置文件 .eslintrc 示例及说明
    JQ ajax全局事件 (个人学习笔记)
    JQ ajax (个人学习笔记)
    js 滚动加载图片(懒加载)实现原理 (个人学习笔记)
    js 正则 (个人学习笔记)
    js 理解闭包 (个人学习笔记)
    js 改变this指向的几种方法(个人学习笔记)
    js 理解this指向(个人学习笔记)
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5023620.html
Copyright © 2020-2023  润新知