Longest Subsequence

题目1 : Longest Subsequence

时间限制:10000ms

单点时限:1000ms

内存限制:256MB

描述

You are given a set of N words W1, W2, ... WN and a target string S. Find the longest word which is also a subsequence of S.

输入

The first line contains an integer N. (1 <= N <= 10000)

The following N lines each contain a word Wi. (1 <= Σ|Wi| <= 100000)

The last line contains the string S. (1 <= |S| <= 100000)

输出

The length of the longest word which is also a subsequence of S.

样例输入

3  
a  
ac  
abc  
acbc

样例输出

3

 

#include<string>
using namespace std;

int main()
{
	int n;
	cin >> n;
	string w[10000],s;
	for (int i = 0; i < n; i++)
		cin >> w[i];
	cin >> s;

	int Max = 0;
	for (int j = 0; j < n; j++)
	{
		int sum = 0;
		for (int k = 0; k < s.length(); k++)
		{
			if (w[j][sum] == s[k])
				sum++;

			if (sum == w[j].length())
				break;
		}
		if (sum == w[j].length())
			Max = Max > sum ? Max : sum;
	}
	cout << Max << endl;
	return 0;
}
package Daily;

import java.util.Scanner;
import java.util.TreeMap;
import  java.util.*;
public class Main_227 {

    static class CharPos implements Comparable<CharPos> {
        char c;
        int pos;

        CharPos(char c, int pos) {
            this.c = c;
            this.pos = pos;
        }

        char getChar() {
            return c;
        }

        int getPos() {
            return pos;
        }

        public int compareTo(CharPos o) {
            if (c == o.c) {
                return pos - o.pos;
            } else if (c < o.c) {
                return -1;
            } else {
                return 1;
            }
        }
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        String a[] = new String[n];
        for (int i = 0; i < n; i++)
        {
            a[i] = sc.next();
        }
        String s = sc.next();

        TreeMap<CharPos, Integer> m = new TreeMap<>();

        for(int i = 0; i < s.length(); i++)
        {
            m.put(new CharPos(s.charAt(i), i), i);
        }


        int ans = 0;
        for (int i = 0; i < n; i++)
        {
            int j;
            int pos = -1;
            for (j = 0; j < a[i].length(); j++)
            {
                CharPos entry = m.higherKey(new CharPos(a[i].charAt(j), pos));

                if (entry == null || entry.getChar() != a[i].charAt(j))
                {
                    pos = -1;
                    break;
                }
                else
                {
                    pos = entry.getPos();
                }
            }

            if (pos != -1)
            {
                ans = Math.max(ans, a[i].length());
            }

        }
        System.out.println(ans);
    }
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值