当前位置:网站首页>Class assignment (6) - 575. Word division (word)
Class assignment (6) - 575. Word division (word)
2022-07-24 15:33:00 【xyc20120615】
Catalog
Description
There is a long string of lowercase letters . To facilitate the analysis of this string , It needs to be divided into several parts , Each part is called a word . For the purpose of reducing the amount of analysis , We want to divide as few words as possible . You are here to complete this division .
Format
Input
The first 1 That's ok , A string .( The length of the string does not exceed 100)
The first 2 Line an integer n, Indicates the number of words .(n<=100)
The first 3∼n+2 That's ok , List one word per line .
Output
An integer , Represents the minimum number of words that a string can be divided into .
Samples
input data 1
realityour
5
real
reality
it
your
our
Output data 1
2
Hint
The original string can be split into real+it+your or reality+our, because reality+our Just two parts , Therefore, the optimal solution is 2, Also note , Each word in the word list can be used repeatedly , It can also be done without
Limitation
1s, 1024KiB for each test case.
Answer key :
It's the deformed knapsack problem , after complex After judgment and loop traversal, find the smallest solution , State transition equation :
dp[k]=min(dp[k],dp[k-a[j].size()]+1);// State transition equation Program :
#include<bits/stdc++.h>
using namespace std;
string a[110],s;
int n,len,len1,dp[110];
int main(){
cin>>s>>n;
len=s.size();// The length of the string
for(int i=0;i<n;i++)
cin>>a[i];
memset(dp,0x3f,sizeof(dp));// Set it to infinity
dp[0]=0;
for(int i=0,k;i<len;i++,k=i+1;/* Traversed position */)
for(int j=0;j<n;j++len1=a[j].size();/* The length of the word */)
if(len1<=k)// Judge whether the length of this word is less than the current traversal position
if(s.substr(k-len1,len1)==a[j])// Determine whether the part of the string is the same as the currently traversed word
dp[k]=min(dp[k],dp[k-a[j].size()]+1);// State transition equation
cout<<dp[len];
return 0;
}边栏推荐
- celery 启动beat出现报错ERROR: Pidfile (celerybeat.pid) already exists.
- MySQL学习笔记(总结)
- How to deal with being attacked? Advanced anti DDoS IP protection strategy
- MySQL build master-slave synchronization - build with docker
- JUC源码学习笔记3——AQS等待队列和CyclicBarrier,BlockingQueue
- MySQL learning notes (summary)
- 三、集合基础——ArrayList集合与简单学生管理系统
- Wildfire STM32 domineering, through the firmware library to achieve water light
- Choice of advanced anti DDoS IP and CDN in case of DDoS
- Memorythrashing: Tiktok live broadcast to solve memory dithering practice
猜你喜欢

Still using listview? Use animatedlist to make list elements move

Do you understand the working principle of gyroscope?

Nine key measures to maintain server security in Hong Kong

Personal practical experience: Data Modeling "whether account data belongs to dimension or account domain"

Jmeter-调用上传文件或图片接口

ZABBIX administrator forgot login password

Research on stability of time-delay systems based on Lambert function

Outlook tutorial, how to create tasks and to DOS in outlook?

【Flutter -- 布局】流式布局(Flow和Wrap)

Leetcode 1288. delete the covered interval (yes, solved)
随机推荐
哈夫曼树(最优二叉树)
【OpenCV 例程300篇】238. OpenCV 中的 Harris 角点检测
Do you understand the working principle of gyroscope?
Vector introduction and underlying principle
[machine learning basics] - another perspective to explain SVM
R语言可视化分面图、多变量分组嵌套多水平t检验、并指定参考水平、可视化多变量分组嵌套多水平分面箱图(faceting boxplot)并添加显著性水平、指定显著性参考水平
(09) flask is OK if it has hands - cookies and sessions
维护香港服务器安全的9个关键措施
2022 RoboCom 世界机器人开发者大赛-本科组(省赛)-- 第五题 树与二分图 (已完结)
Jmeter-调用上传文件或图片接口
JS native - array sorting to find out the characters with the largest number of strings
Huawei camera capability
Leetcode-09 (next rank + happy number + full rank)
上课作业(6)——#575. 单词的划分(word)
Choice of advanced anti DDoS IP and CDN in case of DDoS
每天20分钟之feign
celery 启动beat出现报错ERROR: Pidfile (celerybeat.pid) already exists.
C# 无操作则退出登陆
Reentrantlock reentrant lock
25. From disk to file