GCPC2014 J Not a subsequence

题意:给你一个字符串,问你含有 k个字符集合 长度最短的不是字符串子序列的种类数和长度。

解题思路:DP.很难想  site[i] 表示以i 开头的使得 串不再字符串中的最小长度 ,dp[i] 表示种类数。  状态转移方程在代码里面。

解题代码:

  1 // File Name: j.4.cpp
  2 // Author: darkdream
  3 // Created Time: 2015年03月20日 星期五 16时43分43秒
  4
  5 #include<vector>
  6 #include<list>
  7 #include<map>
  8 #include<set>
  9 #include<deque>
 10 #include<stack>
 11 #include<bitset>
 12 #include<algorithm>
 13 #include<functional>
 14 #include<numeric>
 15 #include<utility>
 16 #include<sstream>
 17 #include<iostream>
 18 #include<iomanip>
 19 #include<cstdio>
 20 #include<cmath>
 21 #include<cstdlib>
 22 #include<cstring>
 23 #include<ctime>
 24 #include<climits>
 25 #define LL long long
 26 #define maxn 1000005
 27 #define M 1000000007
 28 using namespace std;
 29 int t, k;
 30 char str[maxn];
 31 int hs[600];
 32 int ok[64];
 33 int nxt[64];
 34 int dp[maxn];
 35 int site[maxn];
 36 int sum[maxn];
 37 int main(){
 38     for(int i = ‘a‘ ;i <= ‘z‘ ;i ++)
 39     {
 40         hs[i] = i - ‘a‘;
 41     }
 42     for(int i = ‘A‘ ;i <= ‘Z‘; i++)
 43     {
 44         hs[i] = i - ‘A‘ + 26;
 45     }
 46     for(int i = ‘0‘ ;i <= ‘9‘; i ++)
 47     {
 48         hs[i] = i - ‘0‘ + 52;
 49     }
 50     scanf("%d",&t);
 51     while(t--)
 52     {
 53         scanf("%d %s",&k,str);
 54         int len = strlen(str);
 55         memset(ok,0,sizeof(int)*k);
 56         sum[0] = 0;
 57         int wei = 0 ;
 58         memset(nxt,-1,sizeof(int)*k);
 59         int twei = 0 ;
 60         int tt;
 61         for(int i = len -1;i >= 0 ;--i)
 62         {
 63             site[i] = wei;
 64             tt = nxt[hs[str[i]]] ;
 65             if(wei){
 66                 sum[wei] = (sum[wei]+sum[wei-1])%M;
 67                 dp[i] = sum[wei-1];
 68                 if(site[tt] != wei)
 69                 {
 70                     sum[wei-1] = (sum[wei-1]-dp[tt]+M)%M;
 71                 }else{
 72                     sum[wei] = (sum[wei]-dp[tt]+M)%M;
 73                 }
 74             }else{
 75                 sum[wei] += k-twei;
 76                 dp[i] = k -twei;
 77                 if(tt != -1)
 78                 {
 79                     sum[wei] -= dp[tt];
 80                 }
 81             }
 82             if(ok[hs[str[i]]] == 0)
 83             {
 84               ok[hs[str[i]]] = 1;
 85               twei ++ ;
 86             }
 87             if(twei == k )
 88             {
 89               wei ++ ;
 90               twei = sum[wei] = 0 ;
 91               memset(ok,0,sizeof(int)*k);
 92             }
 93             nxt[hs[str[i]]] = i ;
 94         }
 95         if(!wei){
 96             printf("1 %d\n",k-twei);
 97         }else{
 98             printf("%d %d\n",wei+1,sum[wei-1]);
 99         }
100     }
101 return 0;
102 }

时间: 2024-12-29 23:23:05

GCPC2014 J Not a subsequence的相关文章

ZOJ 3603 DP LCS

已经5年没有做OJ了, 曾经沧海难为水,除去巫山不是云" 准备每周刷1-2题! 题目大意:给出N个字符串,且各个字符串都包含唯一的字母,即不存在"ABCA"(A重复了),而"AFDSG"是正确的.                  求出N个字符串的公共字母. 最后,按照字典序输出. 分     析:首先对各个字符串进行字典序排序,然后求所有的LCS,做法是两两相求即可.N个字符串,总共求N-1次LCS,就得到最后的结果了. 代     码: //http:

PAT 1007

1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <= K. The

115 Distinct Subsequences

115 Distinct Subsequences 这道题是dp, 我定义的dp[i][j] 为用t[:i] 和s[:j] 的disitinct subsequence排列数 class Solution: # @param {string} s # @param {string} t # @return {integer} def numDistinct(self, s, t): m, n = len(t), len(s) if m == 0 or n == 0: return 0 dp,cu

Distinct Subsequences -- from Leetcode

Problem: Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

Longest Common Subsequence

Problem statement: Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. Have you met this question in a real interview? Yes Clarification What's the definition of Longest Common Subsequence? https:/

中南OJ1551: Longest Increasing Subsequence Again(分块+离散化线段树)

1551: Longest Increasing Subsequence Again Time Limit: 2 Sec  Memory Limit: 256 MB Submit: 29  Solved: 15 [Submit][Status][Web Board] Description Give you a numeric sequence. If you can demolish arbitrary amount of numbers, what is the length of the

Common Subsequence

A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..., xm > another sequence Z = < z1, z2, ..., zk > is a subsequence of X if there exists a strictly increasing

300. Longest Increasing Subsequence

Problem statement: Given an unsorted array of integers, find the length of longest increasing subsequence. For example,Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there