zoukankan      html  css  js  c++  java
  • 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");
                }
            }
        }
    }
  • 相关阅读:
    mysqldump 导出数据库为DBname的表名为Tname的表结构 导出数据库的所有表的表结构
    mysqldump 备份某张表 Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions,
    nfs missing codepage or helper program, or other error
    date 增加一个小时 减少一个小时
    mysqldump 备份单个数据库
    mysql删除账户
    怎么删除某个用户的所有帖子?
    mongodb删除重复数据
    ReSharper2018破解详细方法
    激活windows和office
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5023620.html
Copyright © 2011-2022 走看看