搜索————可以练习一手

问题 D: Team Name
时间限制: 1 Sec 内存限制: 128 MB

题目描述

After all the teams have been matched, what to do next is of course to think about a nice team name. Now it is known that there are n teams taking part in the Guangxi Province Collegiate Programming Contest. And the name of every team is a string consists of English lower characters. Now Luras needs to get her team name, she doesn’t want the name be any consecutive substring of any other teams. And she prefers shorter names. If there are many choices with the shortest length, she would prefer the one which is the smallest lexicographically. Now could you decide the team name for her? We regard string a is lexicographically smaller than string b if there exists such index j that a[i] == b[i] for all i < j and a[j] < b[j].

输入

The first line is an integer T which indicates the case number.
And as for each case, there will be n + 1 lines.
In the first line, there is one integer n, which indicates the number of teams.
Then there will be n strings of the next n lines, indicate the name of every team in each line.

It is guaranteed that——
T is about 100.
for 100% cases, 1 <= n <= 100, 1 <= |s|(the length of s)<= 30.

输出

As for each case, you need to output a single line.
There should be one string in the line which means the name Luras will give to her team.

样例输入

2
3
a
b
c
2
abcdefghijklmnopqrstuvwxyz
aa

题目是让我们求不属于题目输入串的子串的一个串,
这个结果串要保证在尽量短的情况下,字典序最小。

ps:题意可能表达的不大清楚,尽量自己看。

需要了解 strstr()函数的使用,
再深搜字符串(我也是第一次见深搜字符串的QAQ)

strstr(T,s);
在主串 T 中是否存在串 s,
找到返回 串s 在主串T 的首地址
找不到就返回 NULL
#include<iostream>
#include<queue>
#include<algorithm>
#include<vector>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<set>
#include<map>
using namespace std;

const int inf = 0x3f3f3f3f;
const int Mod = 1e9+7;
const int N = 40;

typedef long long ll;
typedef pair<int ,int > pii;

char ans[N];
char M[105][N];
bool flag = false;

void dfs(int dep,int len,int m){
    if(len == dep){
        ans[len++] = '\0';   // 防止后面有多余的部分被输出
        int con = 0;
        for(int i = 0 ;i < m;i++)
         if(!strstr(M[i],ans)) con++;  // 找不在的,搞错了ya
        if(con == m) flag = true; 
        return ;
    }
    for(int  i = 0;i < 26;i++){
        
        ans[len]= 'a'+i;
        dfs(dep,len+1,m);
        if(flag) return;
    }
}

// 我是sb,曲解了题意.而且还没想到用dfs and strstr()  函数
void  D(){
   int n;
   cin>>n;
   for(int i = 0;i < n;i++){
       scanf("%s",M[i]);
       //printf("%s\n",M[i]);
   }
   int dep = 1;
  flag = false;
  while(!flag){
      dfs(dep,0,n);
     // flag =true;
      dep++;    //可能长度为 1 就可,但也可能是样例中ac,即长度为2 
  }
  printf("%s\n",ans);
}
int main(){
  int T;
  cin>>T;
  while(T--) D();
  return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值