D - Send More Money
Problem Statement
Given strings S1,S2,S3 consisting of lowercase English letters, solve the alphametic S1+S2=S3.
Formally, determine whether there is a triple of positive integers N1,N2,N3 satisfying all of the three conditions below, and find one such triple if it exists.
Here, N1′,N2′,N3′ are strings representing N1,N2,N3 (without leading zeros) in base ten, respectively.
Ni′ and S_iSi have the same number of characters.
N1+N2=N3.
The xx-th character of Si and the yy-th character of Sj is the same if and only if the xx-th character of Ni′ and the yy-th character of Nj′ are the same.
Constraints
Each of S1, S2, S3 is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.
Input
Input is given from Standard Input in the following format:
S1 S2 S3
Output
If there is a triple of positive integers N1,N2,N3 satisfying the conditions, print one such triple, using newline as a separator. Otherwise, print UNSOLVABLE instead.
Sample Input 1 Copy
a
b
c
Sample Output 1 Copy
1
2
3
Outputs such as (N1,N2,N3)=(4,5,9) will also be accepted, but (1,1,2) will not since it violates the third condition (both a and b correspond to 1).
Sample Input 2 Copy
x
x
y
Sample Output 2 Copy
1
1
2
Outputs such as (N1,N2,N3)=(3,3,6) will also be accepted, but (1,2,3) will not since it violates the third condition (both 1 and 2 correspond to x).
Sample Input 3 Copy
p
q
p
Sample Output 3 Copy
UNSOLVABLE
Sample Input 4 Copy
abcd
efgh
ijkl
Sample Output 4 Copy
UNSOLVABLE
Sample Input 5 Copy
send
more
money
Sample Output 5 Copy
9567
1085
10652
#include<stdio.h>
#include<string.h>
#include<math.h>
char S[5][15],str[15];
long long int N1,N2,N3;
int l1,l2,l3,l,flag,k;
int N[30],book[30];
void fun(int num,int len)
{
for(int i=0;i<len;i++)
{
flag=1;
for(int j=0;j<l;j++)
{
if(str[j]==S[num][i])
{flag=0;break;}
}
if(flag==1)
str[l++]=S[num][i];
}
}//找出字符串中共有多少个字母
long long task(int num,int len)
{
long long n=0,m=0;
for(int i=0;i<len;i++)
{
for(int j=0;j<l;j++)
{
if(S[num][i]==str[j])
{
if(i==0&&N[j]==0)
return -1;
else
{
m=N[j];
for(int k=0;k<len-i-1;k++)
m*=10;
n+=m;
}
}
}
}
return n;
}//计算N1,N2,N3
void dfs(int x)
{
if(x==l)
{
N1=task(1,l1);
N2=task(2,l2);
N3=task(3,l3);
if(N1<0||N2<0||N3<0)
return ;
if(N1+N2==N3)
{
if(k==0)
printf("%lld\n%lld\n%lld\n",N1,N2,N3);
k++;
}
return ;
}
for(int i=0;i<=9;i++)
{
if(book[i]==0)
{
N[x]=i;
book[i]=-1;
dfs(x+1);
book[i]=0;
}
else
continue;
}
}
int main()
{
scanf("%s %s %s",S[1],S[2],S[3]);
l1=strlen(S[1]);
l2=strlen(S[2]);
l3=strlen(S[3]);
fun(1,l1);
fun(2,l2);
fun(3,l3);
if(l>10)
printf("UNSOLVABLE\n");
else
{
dfs(0);
if(k==0)
printf("UNSOLVABLE\n");
}
return 0;
}
题目大意就是给你三个字符串,让我们找对应的三个整数,每个字符是一位数字,满足N1+N2=N3.我的大致思路是找出三个字符串共有多少字符,如果超过10个就之间UNSOLVABLE,注意可以等于10的,就是在这个地方蹉跎了一天。再用dfs把找出每个字符的可能,去除首字符为0的,剩下满足的任意输出一位就可以了。
P2957 [USACO09OCT]Barn Echoes G
题目描述
The cows enjoy mooing at the barn because their moos echo back, although sometimes not completely. Bessie, ever the excellent
secretary, has been recording the exact wording of the moo as it goes out and returns. She is curious as to just how much overlap there is.
Given two lines of input (letters from the set a..z, total length in the range 1..80), each of which has the wording of a moo on it, determine the greatest number of characters of overlap between one string and the other. A string is an overlap between two other strings if it is a prefix of one string and a suffix of the other string.
By way of example, consider two moos:
moyooyoxyzooo
yzoooqyasdfljkamo
The last part of the first string overlaps 'yzooo' with the first part of the second string. The last part of the second string
overlaps 'mo' with the first part of the first string. The largest overlap is 'yzooo' whose length is 5.
POINTS: 50
奶牛们非常享受在牛栏中哞叫,因为她们可以听到她们哞声的回音。虽然有时候并不能完全听到完整的回音。Bessie曾经是一个出色的秘书,所以她精确地纪录了所有的哞叫声及其回声。她很好奇到底两个声音的重复部份有多长。
输入两个字符串(长度为1到80个字母),表示两个哞叫声。你要确定最长的重复部份的长度。两个字符串的重复部份指的是同时是一个字符串的前缀和另一个字符串的后缀的字符串。
我们通过一个例子来理解题目。考虑下面的两个哞声:
moyooyoxyzooo
yzoooqyasdfljkamo
第一个串的最后的部份"yzooo"跟第二个串的第一部份重复。第二个串的最后的部份"mo"跟第一个串的第一部份重复。所以"yzooo"跟"mo"都是这2个串的重复部份。其中,"yzooo"比较长,所以最长的重复部份的长度就是5。
输入格式
* Lines 1..2: Each line has the text of a moo or its echo
输出格式
* Line 1: A single line with a single integer that is the length of the longest overlap between the front of one string and end of the other.
输入输出样例
输入 #1
abcxxxxabcxabcd
abcdxabcxxxxabcx
输出 #1
11
说明/提示
'abcxxxxabcx' is a prefix of the first string and a suffix of the second string.
#include<stdio.h>
#include<string.h>
int max(int x,int y)
{
return (x>y)?x:y;
}
int main()
{
char s1[100],s2[100];
scanf("%s %s",s1,s2);
int l1,l2,fro=-1,aft=-1,len,i,j,k;
l1=strlen(s1);
l2=strlen(s2);
for(i=0;i<l2;i++)
{
if(s2[i]==s1[0])
{
len=0;
int x,y=1,flag=1;
for(x=i+1;x<l2;x++)
if(s2[x]!=s1[y++])
{flag=0;break;}
if(x>l2-1&&flag==1)
len=l2-i;
fro=max(fro,len);
}
}
for(i=0;i<l1;i++)
{
if(s1[i]==s2[0])
{
len=0;
int x=i+1,y=1,flag=1;
for(x=i+1;x<l1;x++)
if(s1[x]!=s2[y++])
{flag=0;break;}
if(x>l1-1&&flag==1)
len=l1-i;
aft=max(aft,len);
}
}
printf("%d",max(fro,aft));
}
还没有搞懂哈希算法,这个题目就按自己的想法写了一下,两个循环找前缀和后缀,输出最大值即可。