hdu 1238 字符串处理 暴力

Substrings

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13779    Accepted Submission(s):
6689

http://acm.hdu.edu.cn/showproblem.php?pid=1238

Problem Description

You are given a number of case-sensitive strings of
alphabetic characters, find the largest string X, such that either X, or its
inverse can be found as a substring of any of the given strings.

Input

The first line of the input file contains a single
integer t (1 <= t <= 10), the number of test cases, followed by the input
data for each test case. The first line of each test case contains a single
integer n (1 <= n <= 100), the number of given strings, followed by n
lines, each representing one string of minimum length 1 and maximum length 100.
There is no extra white space before and after a string.

Output

There should be one line per test case containing the
length of the largest string found.

Sample Input

2
3
ABCD
BCDFF
BRCD
2
rose
orchid

Sample Output

2 2

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=100+3;
char str[maxn];
string ansstr,ss;
string str2[maxn];
int t,n;
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%s",str);
            str2[i]=str;
        }
        string temp=str2[0];
        int len=temp.size();
        int ans=0;
        for(int i=0;i<len;i++){
            for(int j=1;j+i<=len;j++){
                int cnt=1;
                ss=temp.substr(i,j);
                for(int k=1;k<n;k++){
                    if(str2[k].find(ss)!=-1){
                        cnt++;
                    }
                }
                if(cnt==n){
                    if(ans<j){
                        ans=j;
                        ansstr=ss;
                    }else if(ans==j){
                        ansstr=min(ansstr,ss);
                    }
                }
            }
        }
        reverse(temp.begin(),temp.end());
        for(int i=0;i<len;i++){
            for(int j=1;j+i<=len;j++){
                int cnt=1;
                ss=temp.substr(i,j);
                for(int k=1;k<n;k++){
                    if(str2[k].find(ss)!=-1){
                        cnt++;
                    }
                }
                if(cnt==n){
                    if(ans<j){
                        ans=j;
                        ansstr=ss;
                    }else if(ans==j){
                        ansstr=min(ansstr,ss);
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
}
#include<stdio.h>//网上大佬的代码
#include<algorithm>
#include<iostream>
#include<string.h>
using namespace std;

const int maxn = 105;
char str[maxn];
string ansstr;
string str2[maxn];

int main()
{
    int n,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%s",str);
            str2[i]=str;
        }
        int ans=0;
        string tmp=str2[0];
        int tmplen=tmp.size();
        for(int i=0;i<tmplen;i++)
        {
            for(int j=1;i+j<=tmplen;j++)
            {
                int cnt=1;
                string ss=tmp.substr(i,j);
                for(int k=1;k<n;k++)
                {
                    if(str2[k].find(ss)!=-1)
                        cnt++;
                }
                if(cnt==n)
                {
                    if(ans<j)
                    {
                        ans=j;
                        ansstr=ss;
                    }
                    else if(ans==j)
                    {
                        ansstr=min(ansstr,ss);
                    }
                }
            }
        }
        reverse(tmp.begin(),tmp.end());//reverse()会将区间[beg,end)内的元素全部逆序;
        for(int i=0;i<tmplen;i++)
        {
            for(int j=1;i+j<=tmplen;j++)
            {
                int cnt=1;
                string ss=tmp.substr(i,j);
                for(int k=1;k<n;k++)
                {
                    if(str2[k].find(ss)!=-1)
                        cnt++;
                }
                if(cnt==n)
                {
                  //  cout<<ss<<" "<<cnt<<endl;
                    if(ans<j)
                    {
                        ans=j;
                        ansstr=ss;
                    }
                    else if(ans==j)
                    {
                        ansstr=min(ansstr,ss);
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
}

原文地址:https://www.cnblogs.com/qqshiacm/p/11599482.html

时间: 2024-08-24 16:19:48

hdu 1238 字符串处理 暴力的相关文章

poj 1226 hdu 1238 Substrings 求若干字符串正串及反串的最长公共子串 2002亚洲赛天津预选题

题目:http://poj.org/problem?id=1226 http://acm.hdu.edu.cn/showproblem.php?pid=1238 其实用hash+lcp可能也可以,甚至可能写起来更快,不过我没试,我最近在练习后缀数组,所以来练手 后缀数组的典型用法之一----------------后缀数组+lcp+二分 思路:1.首先将所有的字符串每读取一个,就将其反转,作为一组,假设其下标为i到j,那么cnt[i]到cnt[j]都标记为一个数字(这个数字意思是第几个读入的字符

hdu 2721(字符串处理,位运算 暴力)

Persistent Bits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 201    Accepted Submission(s): 116 Problem Description WhatNext Software creates sequence generators that they hope will produce

HDU 1238

好吧,这题直接搜索就可以了,不过要按照长度最短的来搜,很容易想得到. 记得ACM比赛上有这道题,呃..不过,直接搜..呵呵了,真不敢想. 1 #include <iostream> 2 #include <cstdio> 3 #include <string.h> 4 #include <algorithm> 5 using namespace std; 6 7 char str[105][105]; 8 char s1[105],s2[105]; 9 10

HDU 4499 Cannon (暴力搜索)

题意:在n*m的方格里有t个棋子,问最多能放多少个炮且每个炮不能互相攻击(炮吃炮) 炮吃炮:在同一行或同一列且中间有一颗棋子. #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <math.h> #define M 50 #define LL long long using

HDU 4902 线段树||暴力

给定一个序列,两种操作 1:把一段变成x. 2:把一段每个数字,如果他大于x,就变成他和x的gcd,求变换完后,最后的序列. 线段树解法:用lazy标记下即可,优化方法还是很巧妙的, Accepted 4902 515MS 3308K 1941 B C++ #include "stdio.h" #include "string.h" struct node { int l,r,x;// 在叶子节点代表值,树节点代表成端更新的lazy操作. }data[400010]

HDU 4831 Scenic Popularity 暴力模拟

Scenic Popularity Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 340    Accepted Submission(s): 110 Problem Description 临近节日,度度熊们最近计划到室外游玩公园,公园内部包括了很多的旅游景点区和休息区,由于旅游景点很热门,导致景点区和休息区都聚集了很多人.所以度度熊

HDU 4961 Boring Sum 暴力

题意:对于所有的A[I],同时找到左边和右边离它最近且是它的倍数的数相乘最后加起来求和. 解题思路:n*sqrt(n)的算法,开始以为过不了,wa了两发因为lld I64d对拍一个小时发现一个小时前交的代码没错只是没变I64d,..具体思路是枚举每个a[i]的因子,找离它最近的那个更新,如果已经没更新就不用更新了.用两个辅助数组记录最近的就行. 解题代码: 1 // File Name: 1002.cpp 2 // Author: darkdream 3 // Created Time: 201

HDU - 2089 不要62 (暴力或数位DP)

Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众. 不吉利的数字为所有含有4或62的号码.例如: 62315 73418 88914 都属于不吉利号码.但是,61152虽然含有6和2,但不是62连号,所以不属于不吉利数字之列. 你的任务是,对于每次给出的一个牌照区间号,推断出交管局今次又要实际上给多少辆新

hdu 4737 二分或暴力

http://acm.hdu.edu.cn/showproblem.php?pid=4737 Problem Description There are n numbers in a array, as a0, a1 ... , an-1, and another number m. We define a function f(i, j) = ai|ai+1|ai+2| ... | aj . Where "|" is the bit-OR operation. (i <= j)