最短前缀
计蒜客 - T1259
一个字符串的前缀是从该字符串的第一个字符起始的一个子串。
例如 “carbon” 的字串是: “c”, “ca”, “car”, “carb”, “carbo”, 和 “carbon”。注意到这里我们不认为空串是子串, 但是每个非空串是它自身的子串. 我们现在希望能用前缀来缩略的表示单词。例如, “carbohydrate” 通常用 “carb” 来缩略表示. 现在给你一组单词, 要求你找到唯一标识每个单词的最短前缀。
在下面的例子中,“carbohydrate” 能被缩略成"carboh", 但是不能被缩略成"carbo" (或其余更短的前缀) 因为已经有一个单词用 “carbo” 开始。
一个精确匹配会覆盖一个前缀匹配,例如,前缀 “car” 精确匹配单词 “car”. 因此 “car” 是 “car” 的缩略语是没有二义性的 , “car” 不会被当成 “carriage” 或者任何在列表中以 “car” 开始的单词。
输入格式
输入包括至少
2
行,至多
1000
行. 每行包括一个以小写字母组成的单词,单词长度至少是
1
,至多是
20
。
输出格式
输出的行数与输入的行数相同。
每行输出由相应行输入的单词开始,后面跟着一个空格接下来是相应单词的没有二义性的最短前缀标识符。
Sample Input
carbohydrate
cart
carburetor
caramel
caribou
carbonic
cartilage
carbon
carriage
carton
car
carbonate
Sample Output
carbohydrate carboh
cart cart
carburetor carbu
caramel cara
caribou cari
carbonic carboni
cartilage carti
carbon carbon
carriage carr
carton carto
car car
carbonate carbona
了解strstr函数用法
还有就是deepl翻译器,从队友拿了解的,是真的好用
代码:
#include<cstdio>
#include<iostream>
#include<string.h>
#define LL long long
#define INF 0x3f3f3f3f
#define Swap(a,b) {int temp=a;a=b;b=temp;}
using namespace std;
const int maxn = 1010;
char a[1002][22],ans[22];//a为输入数据;ans为输出数据
char len[1002];//len储存长度
int main()
{
int n,i = 0;
while(cin >> a[i]) {
len[i] = strlen(a[i]);
i ++;
}
int k;
for(int j=0;j<i;j++) {
//int k;
for(k=1;k<=len[j];k++) {
memset(ans,0,sizeof(ans));
int l;
for(l=0;l<k;l++) {
ans[l] = a[j][l];
}
//ans[l]='\0';
for(l=0;l<i;l++) {
if(l != j) {
if(strstr(a[l],ans) == a[l]) {//这里不能写成!=NULL,应该满足前缀a[l];
break;
}
}
}
if(l == i) {
cout<<a[j]<<" "<<ans<<endl;
break;
}
}
if(k == len[j] + 1) cout<<a[j]<<" "<<a[j]<<endl;
}
return 0;
}
POJ - 3126
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices.
— It is a matter of security to change such things every now and then, to keep the enemy in the dark.
— But look, I have chosen my number 1033 for good reasons. I am the Prime minister, you know!
— I know, so therefore your new number 8179 is also a prime. You will just have to paste four new digits over the four old ones on your office door.
— No, it’s not that simple. Suppose that I change the first digit to an 8, then the number will read 8033 which is not a prime!
— I see, being the prime minister you cannot stand having a non-prime number on your door even for a few seconds.
— Correct! So I must invent a scheme for going from 1033 to 8179 by a path of prime numbers where only one digit is changed from one prime to the next prime.
Now, the minister of finance, who had been eavesdropping, intervened.
— No unnecessary expenditure, please! I happen to know that the price of a digit is one pound.
— Hmm, in that case I need a computer program to minimize the cost. You don’t know some very cheap software gurus, do you?
— In fact, I do. You see, there is this programming contest going on… Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero, of course. Here is a solution in the case above.
1033
1733
3733
3739
3779
8779
8179
The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased.
Input
One line with a positive number: the number of test cases (at most 100). Then for each test case, one line with two numbers separated by a blank. Both numbers are four-digit primes (without leading zeros).
Output
One line for each case, either with a number stating the minimal cost or containing the word Impossible.
Sample Input
3
1033 8179
1373 8017
1033 1033
Sample Output
6
7
0
代码;
#include<iostream>
#include<cstdio>
#include<queue>
#include<string.h>
#define LL long long
#define INF 0x3f3f3f3f
#define Swap(a,b) {int temp=a;a=b;b=temp;}
using namespace std;
const int maxn = 1010;
int mp[10][10][10][10];
struct node {
int x,y,z,k;
int step;
};
int a,b,c,d;
int A,B,C,D;
void swap1(int n,int m) {//转换位
a = n / 1000;
n = n % 1000;
b = n / 100;
n = n % 100;
c = n / 10;
n = n % 10;
d = n;
A = m / 1000;
m = m % 1000;
B = m / 100;
m = m % 100;
C = m / 10;
m = m % 10;
D = m;
}
bool JudgeP(node T) {//判断素数
int p = T.x * 1000 + T.y * 100 + T.z * 10 + T.k;
for(int i=2;i*i<=p;i++) {
if(p % i == 0) return false;
}
return true;
}
int bfs(int n,int m) {
//memset(mp,0,sizeof(mp));
queue<node> Q;
swap1(n,m);
node now,next;
now.x = a;
now.y = b;
now.z = c;
now.k = d;
mp[a][b][c][d] = 1;
now.step = 0;
Q.push(now);
while(!Q.empty()) {
now = Q.front();
Q.pop();
if(now.x == A && now.y == B && now.z == C && now.k == D) return now.step;
for(int i=1;i<10;i++) {//千位数从1开始
next = now;
next.x = i;
next.step ++;
if(mp[next.x][next.y][next.z][next.k]) continue;
if(!JudgeP(next)) continue;
mp[next.x][next.y][next.z][next.k] = 1;
Q.push(next);
}
for(int i=0;i<10;i++) {
next = now;
next.y = i;
next.step ++;
if(mp[next.x][next.y][next.z][next.k]) continue;
if(!JudgeP(next)) continue;
mp[next.x][next.y][next.z][next.k] = 1;
Q.push(next);
}
for(int i=0;i<10;i++) {
next = now;
next.z = i;
next.step ++;
if(mp[next.x][next.y][next.z][next.k]) continue;
if(!JudgeP(next)) continue;
mp[next.x][next.y][next.z][next.k] = 1;
Q.push(next);
}
for(int i=0;i<10;i++) {//个位数也可以从1开始减少运算
next = now;
next.k = i;
next.step ++;
if(mp[next.x][next.y][next.z][next.k]) continue;
if(!JudgeP(next)) continue;
mp[next.x][next.y][next.z][next.k] = 1;
Q.push(next);
}
}
return -1;
}
int main()
{
int t;
cin >> t;
while(t--) {
int n,m,a,b,c,d;
cin >> n >> m;
memset(mp,0,sizeof(mp));//清空
//swap1(n,m);
int ans = bfs(n,m);
if(ans == -1) cout<<"Impossible"<<endl;
else cout<<ans<<endl;
}
return 0;
}