[String processing + motion gauge] word Division
Time Limit: 1000 ms
Memory limit: 2560kb
There is a long string consisting of lowercase letters. To facilitate the analysis of this string, you need to divide it into several parts, each of which is called a word. To reduce the analysis volume, we hope that the fewer words we divide, the better. You are here to complete this division.
Input Format
The first line is a string. (The length of a string cannot exceed 100)
The second row contains an integer N, indicating the number of words. (N <= 100)
3rd ~ N + 2 rows. Each row lists one word.
Output Format
An integer that represents the minimum number of words that a string can be divided.
Sample Input
Realityour
5
Real
Reality
It
Your
Our
Sample output
2
(The original string can be split into real + It + Your or reality + our. Since reality + our is only two parts, the optimal solution is 2. Note that, each word in the word list can be used multiple times or not)
Status: F [I] indicates the minimum number of splits to the end of F [I ].
State transition equation: If (F [I] = 0) f [I] = f [m] + 1; else f [I] = min (F [I], f [m] + 1 );
The data for this question is weak.
It is in the same status as a hungry ox.
# include<stdio.h># include<cstring># include<vector># include<iostream># include<algorithm>using namespace std;const int maxn=1000;vector<int>q[300];char st[maxn],dp[maxn][maxn];int f[maxn];int check(int a,int b,int c){ if(c-dp[q[a][b]][0]+1<=0)return 0; for(int i=1,j=c-dp[q[a][b]][0]+1;i<=dp[q[a][b]][0]&&j>=1;i++,j++) if(dp[q[a][b]][i]!=st[j])return 0; return 1;}int main(){ scanf("%s\n",st+1); st[0]=strlen(st+1); int n;scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%s",dp[i]+1); dp[i][0]=strlen(dp[i]+1); q[dp[i][dp[i][0]]].push_back(i); } for(int i=1;i<=st[0];i++) for(int j=0;j<q[st[i]].size();j++) if(check(int(st[i]),j,i)){ int m=i-dp[q[st[i]][j]][0]; if(f[i]==0)f[i]=f[m]+1; else f[i]=min(f[i],f[m]+1); } printf("%d",f[st[0]]); return 0;}
[String processing + motion gauge] word Division